How to get div by class in HtmlAgilityPack?

c# html-agility-pack windows-phone-8

Question

I'm following this tutorial, but I have a problem, I don't know how to get htmlNode by class name .

HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.OptionFixNestedTags = true;
htmlDoc.LoadHtml(e.Result);
HtmlNode divContainer = htmlDoc.GetElementbyId("directoryItems");//My problem here,I want to get by class name html
if (divContainer != null)
{
     HtmlNodeCollection nodes = divContainer.SelectNodes("//table/tr");
     ....
}

Popular Answer

Assuming that you want to select a <div> element having class attribute value equals "directoryItems", and you know there will be only one element meets the criteria (or you want to simply select the first occurrence if there are more then one), you can use .SelectSingleNode() method with following XPath query :

HtmlNode divContainer = htmlDoc.DocumentNode
                               .SelectSingleNode("//div[@class='directoryItems']");



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