How to get inner text separately without child tags using HtmlAgilityPack?

I have an HTML page as shown below. I need to take "blah blah blah" only from the "span" tag.

<span class="news"> blah blah blah <div>hello</div> <div>bye</div> </span> 

This gives me all the meanings:

 div.SelectSingleNode(".//span[@class='news']").InnerText.Trim(); 

This gives me null:

 div.SelectSingleNode(".//span[@class='news']/preceding-sibling::text()").InnerText.Trim(); 

How to get text before div tag using HtmlAgilityPack?

+5
source share
1 answer

Your second attempt was pretty close. Use /text() instead of /preceding-sibling::text() , because the text node is a child of span[@class='news'] not sibling (neither the previous nor the next):

 div.SelectSingleNode(".//span[@class='news']/text()") .InnerText .Trim(); 
+8
source

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


All Articles