HTMLAgilityPack analysis in InnerHTML

<div> <b>Token1</b> Token2 <b>Token3</b> </div> 

I am trying to extract token2 from div

I manage to get Token1 and Token3 with:

 HtmlNodeCollection headerFooter = doc.DocumentNode.SelectNodes("//div//b"); 

How can I extract Token2 directly using HTMLAgilityPack?

One dirty option is to replace Token1 and Token2 with string.empty in doc.DocumentNode.SelectNodes ("// div"). InnerText, but I think it can be done in a cleaner way using HTMLAgilityPack ...

+2
source share
1 answer

The text is in text nodes; therefore you should be able to look at "// div / text ()" and combine:

 StringBuilder sb = new StringBuilder(); foreach (HtmlAgilityPack.HtmlTextNode node in doc.DocumentNode.SelectNodes("//div/text()")) { sb.Append(node.Text.Trim()); } string s = sb.ToString(); 
+6
source

Source: https://habr.com/ru/post/1346000/


All Articles