【发布时间】:2009-10-05 00:44:55
【问题描述】:
codeplex 上的例子是这样的:
HtmlDocument doc = new HtmlDocument();
doc.Load("file.htm");
foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"])
{
HtmlAttribute att = link["href"];
att.Value = FixLink(att);
}
doc.Save("file.htm");
第一个问题是HtmlDocument。DocumentElement不存在!确实存在的是 HtmlDocument.DocumentNode 但即使我使用它,我也无法访问所描述的 href 属性。我收到以下错误:
Cannot apply indexing with [] to an expression of type 'HtmlAgilityPack.HtmlNode'
这是我收到此错误时尝试编译的代码:
private static void ChangeUrls(ref HtmlDocument doc)
{
foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//@href"))
{
HtmlAttribute attr = link["href"];
attr.Value = Rewriter(attr.Value);
}
}
更新:我刚刚发现该示例永远不会起作用...阅读示例代码后我得到了解决方案...我会为其他人发布我的解决方案像我一样享受一旦完成。
【问题讨论】:
标签: c# xpath uri html-agility-pack