我如何使用HTMLAgilityPack来提取我想要的值

时间:2010-12-30 17:12:11

标签: c# html-parsing html-agility-pack

对于给定的HTML,我想要id

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

更新 这就是我现在所拥有的

    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);
    }

我得到的错误是在foreach行说明:Object reference not set to an instance of an object.我认为这部分是错误的"//div[@class='name' and @id]"

1 个答案:

答案 0 :(得分:1)

从示例页面修改:

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
}