views:

214

answers:

1

Hi all,

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

A: 

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.

AakashM
That didn't work at first but I brought my selector up a few levels and was finally pointing at a child of the HtmlWrapper node. How did you know it needed a child specificly and not a decendant?
Denis Hoctor
@Denis: found the source via a google for HtmlNodeCollection InsertAfter, noticed that the member being indexed into was called '_childNodes', guessed :)
AakashM