Determine if a node exists in HTMLAgiltyPack

html-agility-pack windows-8.1 windows-phone-8.1

Question

I want run a foreach loop in a HtmlNode which has been parsed from internet via HtmlWebclass and loadFromWebAsync method. Before running the loop I want to make sure the that the node exists in the HtmlDocument. How do I check that without the help Xpath query because many of the Windows RT and Windows 8.1 version doesn't work with this.

Popular Answer

You can use LINQ .Any() method to check if sequence contains any element, for example :

var doc = new HtmlDocument();
.....
var isDivExist = doc.DocumentNode
                    .Descendants("div")
                    .Any();

Or to check if any node in the sequence satisfies specific condition :

var isDivWithSpecificClassExist = doc.DocumentNode
                                     .Descendants("div")
                                     .Any(d => .GetAttributeValue("class", "") == "foo");



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