我应该如何使用 HTMLAgilityPack AppendNode?

发布于 2024-08-18 15:20:38 字数 1237 浏览 3 评论 0原文

周五这个阶段真是头疼!我正在尝试使用 InsertAfter() 将 HtmlNode 添加到另一个。当我将其 rpint 到控制台时,我可以看到带有面包屑 id 的 refChild 节点,但不断收到以下错误:

System.ArgumentOutOfRangeException: Node "<div id="breadcrumb"></div>" was not f
ound in the collection
Parameter name: node
   at HtmlAgilityPack.HtmlNodeCollection.get_Item(HtmlNode node)
   at HtmlAgilityPack.HtmlNode.InsertAfter(HtmlNode newChild, HtmlNode refChild)

   at MyHome.Tasks.Tasks.DownloadandStoreContent(KeyValueP
air`2 urlPair, String filePath, HtmlNode HtmlWrapper) in C:\Users\denis\Document
s\Visual Studio 2008\Websites\MyHomeV2\MyHome.Tasks\Tasks.cs:line 81
   at MyHome.Tasks.Tasks.GenerateContent(String scrape
sSwitch, String filePath) in C:\Users\denis\Documents\Visual Studio 2008\Website
s\MyHomeV2\MyHome.Tasks\Tasks.cs:line 27
   at MyHome.Tasks.Program.Main(String[] args) in C:\Users\denis\Documents\Visua
l Studio 2008\Websites\MyHomeV2\MyHome.Tasks\Program.cs:line 87

我的代码是:

HtmlWrapper.InsertAfter(ContentNode, HtmlWrapper.SelectSingleNode("//div[@id='breadcrumb']"));

正如前面提到的,我已经打印出了 HtmlWrapper 和 HtmlWrapper.SelectSingleNode("//div[@id= 'breadcrumb']") 到控制台并可以在屏幕上看到该节点。关于我在这里出错的地方有什么想法吗?

谢谢, 丹尼斯

Got a real headache at this stage on a Friday! I'm trying to add a HtmlNode to another using InsertAfter(). I can see the refChild node with id of breadcrumbs when I rpint it to the console but keep getting the following error:

System.ArgumentOutOfRangeException: Node "<div id="breadcrumb"></div>" was not f
ound in the collection
Parameter name: node
   at HtmlAgilityPack.HtmlNodeCollection.get_Item(HtmlNode node)
   at HtmlAgilityPack.HtmlNode.InsertAfter(HtmlNode newChild, HtmlNode refChild)

   at MyHome.Tasks.Tasks.DownloadandStoreContent(KeyValueP
air`2 urlPair, String filePath, HtmlNode HtmlWrapper) in C:\Users\denis\Document
s\Visual Studio 2008\Websites\MyHomeV2\MyHome.Tasks\Tasks.cs:line 81
   at MyHome.Tasks.Tasks.GenerateContent(String scrape
sSwitch, String filePath) in C:\Users\denis\Documents\Visual Studio 2008\Website
s\MyHomeV2\MyHome.Tasks\Tasks.cs:line 27
   at MyHome.Tasks.Program.Main(String[] args) in C:\Users\denis\Documents\Visua
l Studio 2008\Websites\MyHomeV2\MyHome.Tasks\Program.cs:line 87

My code is:

HtmlWrapper.InsertAfter(ContentNode, HtmlWrapper.SelectSingleNode("//div[@id='breadcrumb']"));

And as mentioned I have printed out both HtmlWrapper and HtmlWrapper.SelectSingleNode("//div[@id='breadcrumb']") to the console and can see the node on the screen. Any ideas on where I'm going wrong here?

Thanks,
Denis

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贱人配狗天长地久 2024-08-25 15:20:38

从对源代码的粗略检查来看,InsertAfter 似乎希望 refChild 成为您调用 InsertAfter 的节点的直接子节点上。由于您正在搜索 div 节点的整个后代轴(使用 //),因此您作为 refChild 传递的实际节点可能是不是 HtmlWrapper直接子

尝试将 HtmlWrapper.SelectSingleNode("//div[@id='breadcrumb']") 拉入变量,然后在其 ParentNode 上调用 InsertAfter代码>.

From a very cursory examination of the source, it looks like InsertAfter wants refChild to be a direct child of the node you invoke InsertAfter on. Since you are searching the entire descendant axis (with //) for your div node, it's possible that the actual node you pass as refChild isn't a direct child of HtmlWrapper.

Try pulling HtmlWrapper.SelectSingleNode("//div[@id='breadcrumb']") into a variable, and then invoking InsertAfter on its ParentNode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文