用于更改链接的 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);
}
}
更新:我刚刚发现该示例从来没有打算工作......而且我有一个阅读示例代码后的解决方案...我将发布我的解决方案,供像我这样的其他人在完成后享受。
The example on codeplex is this :
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");
The first issue is HtmlDocument.DocumentElement does not exist! What does exist is HtmlDocument.DocumentNode but even when I use that instead, I'm unable to access the href attribute as described. I get the following error:
Cannot apply indexing with [] to an expression of type 'HtmlAgilityPack.HtmlNode'
Here's the code I'm trying to compile when I get this error:
private static void ChangeUrls(ref HtmlDocument doc)
{
foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//@href"))
{
HtmlAttribute attr = link["href"];
attr.Value = Rewriter(attr.Value);
}
}
UPDATE: I Just found that the example was never meant to work...And I've got a solution after reading the example code...I'll post my solution for other people like me to enjoy once completed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我基于 ZIP 中包含的部分示例代码的快速解决方案。
Here's my quick solution based on portions of the sample code included in the ZIP.