Direct Descendants with html agility pack

.net html-agility-pack

Question

I have a specific html node and i want to get the 2nd aka last direct descendant. So after writing .Descendants("div") i wrote ls.Last(). I actually got the last div in the 2nd descendant. Not what i am expecting. How do i get only the direct descendants? or how do i get the descendant with a specific classname? because "div.postBody" would be a suitable alternative.

Accepted Answer

Using XPath would give you what you want. So for direct descendants only you can use -

htmlNode.SelectNodes("/div");

Or if you need to search by specific classname then -

htmlNode.SelectNodes("//div[@class='postBody']");


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why