开发者

How would I use HTMLAgilityPack to extract the value I want

开发者 https://www.devze.com 2023-02-01 15:50 出处:网络
For the given HTML I want the value of id <div class=\"name\" id=\"john-5745844\"> <div class=\"name\" id=\"james-6940673\">

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

    HtmlDocum开发者_StackOverflow中文版ent 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]"


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号