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

c# html-agility-pack html-parsing

對於給定的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]"

一般承認的答案

從示例頁面修改:

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
}


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow