How would I use HTMLAgilityPack to extract the value I want

c# html-agility-pack html-parsing

Question

For the given HTML I want the value of id

 <div class="name" id="john-5745844">
 <div class="name" id="james-6940673">

UPDATE This is what I have at the moment

    HtmlDocument htmlDoc = new HtmlDocument();
    htmlDoc.Load(new StringReader(pageResponse));
    HtmlNode root = htmlDoc.DocumentNode;

    List<string> anchorTags = new List<string>();
    foreach (HtmlNode div in root.SelectNodes("//div[@class='name' and @id]"))
    {
        HtmlAttribute att = div.Attributes["id"];
        Console.WriteLine(att.Value);
    }

The error I am getting is at the foreach line stating: Object reference not set to an instance of an object. I believe the this part is wrong "//div[@class='name' and @id]"

Accepted Answer

Modified from the examples page:

HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm"); //or whatever HTML file you have
foreach(HtmlNode div in doc.DocumentNode.SelectNodes("//div[@class='name' and @id]")
{
   HtmlAttribute att = div["id"];
   //Do something with att.Value
}



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