HtmlAgilityPack: How to interpret non-tag-ed text in HTML
See the question and my original answer on StackOverflowThere 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();
doc.Load(yourHtmlFile);
foreach (HtmlNode node in doc.DocumentNode.SelectNodes("/div/div[starts-with(text(), 'title')]/following-sibling::text()[normalize-space(.) != '']"))
{
Console.WriteLine(node.InnerText.Trim());
}
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.