What is the best way to utilize HTMLAgilityPack AppendNode?

c# html-agility-pack screen-scraping

Question

At this point on a Friday, I've got a serious headache! I'm attempting to use InsertAfter to add a HtmlNode to another (). When I rpint the refChild node with the id breadcrumbs to the console, I can see it, but I keep receiving 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']"));

Additionally, as previously noted, I printed both HtmlWrapper and HtmlWrapper. the node on the screen by typing SelectSingleNode("/div[@id='breadcrumb']") into the terminal. Any suggestions as to where I could be going wrong?

Thanks, Denis

1
7
1/22/2010 3:46:18 PM

Accepted Answer

Looking very quickly at the code, it seems thatInsertAfter wants refChild to descend from the node you call at directInsertAfter on. Considering that you are looking through the full descendent axis (with// (for yourdiv node, it's conceivable that the node you pass as the real noderefChild is not an example ofHtmlWrapper .

Try yankingHtmlWrapper.SelectSingleNode("//div[@id='breadcrumb']") into a variable, after which callingInsertAfter on itsParentNode .

8
1/22/2010 3:56:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow