对于给定的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]"
答案 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
}