I have an HTML page like below. I need to take the 'blah blah blah' alone from the 'span' tag.
<span class="news">
blah blah blah
<div>hello</div>
<div>bye</div>
</span>
This gives me all values:
div.SelectSingleNode(".//span[@class='news']").InnerText.Trim();
This gives me null:
div.SelectSingleNode(".//span[@class='news']/preceding-sibling::text()").InnerText.Trim();
How do I get the text before the 'div' tag using HtmlAgilityPack?
Your 2nd try was pretty close. Use /text()
instead of /preceding-sibling::text()
, because the text node is child of the span[@class='news']
not sibling (neither preceding nor following) :
div.SelectSingleNode(".//span[@class='news']/text()")
.InnerText
.Trim();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With