Take the 2-minute tour ×

Know that the title is kind of vague, here is an example,


How can I fetch line1<br/>line2<br/> for title1, and line2.1<br/>line2.2<br/> for title2? I'm using HtmlAgilityPack and SharpQuery together.


Possible Resolution

After researched and tried more, I managed to fetch these by using LinePosition and "//div/text()"

public static HtmlNodeCollection getNodes(string html, string xpath)
    if (html.Length <= 0) { return null; }
    HtmlDocument doc = new HtmlDocument();
    return doc.DocumentNode.SelectNodes(xpath);

foreach (HtmlNode node in getNodes(html, "//div"){
    foreach (HtmlNode plain_node in getNodes(html, "//div/text()")
        if (plain_node.LinePosition <= node.LinePosition)
            currentHtml += plain_n.InnerHtml + "<br/>";

Any other better way?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

There is rarely one unique solution to an html matching problem. Although your solution works fine now and with your sample, the //div expression will search all div elements under root, recursively.

It means if the original Html evolves somehow, you may catch too many things or analyze too many nodes (performance may be an issue with things like // for big documents).

I would suggest something like this, which is more discriminant:

        HtmlDocument doc = new HtmlDocument();

        foreach (HtmlNode node in doc.DocumentNode.SelectNodes("/div/div[starts-with(text(), 'title')]/following-sibling::text()[normalize-space(.) != '']"))

It means

  • Search div elements from the root
  • Then search div elements underneath
  • Filter (using [...]) these elements and only select those with an inner text that start with 'title'
  • Then search all following sibling elements of type text
  • Filter these elements and only select those which are not empty or whitespace only

See this link for some help on XPATH Axes.

share|improve this answer
Thanks, I was expecting this kind of resolution, I'm lack of systematic knowledge of XPath, thanks for the link. vote up and set as answer. thanks again. I'm also thinking of getting some definitive book about it in near future. –  Elaine Jun 28 '11 at 7:32

Assuming the structure is always the same you could get the divs and then get both of their NextSiblings

share|improve this answer
I updated the post, thanks. –  Elaine Jun 26 '11 at 11:57

Your Answer


