Parsing HTML page with HtmlAgilityPack to select Divs by class

c# html html-agility-pack

Question

I am using C# with HtmlAgilityPack and I can select divs that have an id of foo

var foos = from foo in htmlDoc.DocumentNode.Descendants("div")
           where foo.Id == "foo" 
           select foo;

but how do I select div's with a class of bar?

Accepted Answer

You can use XPATH like this

//div[@class='bar'] 

or

//*/div[@class='bar']

You also may be able to do && foo.Class == "bar".




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