用于更改链接的HtmlAgilityPack示例不起作用。 我该如何做到这一点?

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

更新:我刚刚发现这个例子从来没有打算工作……而且我在阅读了示例代码后得到了一个解决方案……我会为像我这样的其他人发布我的解决方案,一旦完成就可以享受。

这是我基于ZIP中包含的示例代码部分的快速解决方案。

 private static void ChangeLinks(ref HtmlDocument doc) { if (doc == null) return; //process all tage with link references HtmlNodeCollection links = doc.DocumentNode.SelectNodes("//*[@background or @lowsrc or @src or @href]"); if (links == null) return; foreach (HtmlNode link in links) { if (link.Attributes["background"] != null) link.Attributes["background"].Value = _newPath + link.Attributes["background"].Value; if (link.Attributes["href"] != null) link.Attributes["href"].Value = _newPath + link.Attributes["href"].Value;(link.Attributes["href"] != null) link.Attributes["lowsrc"].Value = _newPath + link.Attributes["href"].Value; if (link.Attributes["src"] != null) link.Attributes["src"].Value = _newPath + link.Attributes["src"].Value; } }