This project has moved and is read-only. For the latest updates, please go here.

Exception when Using 'DocumentNode.InsertAfter'

Topics: Developer Forum, Project Management Forum, User Forum
Jan 22, 2009 at 9:34 PM
Hi, I'm trying to do the following:
Look for a specific 'tag' in the HML page and insert a new tag after, I'm trying to do it like the following:

HtmlWeb hw = new HtmlWeb();
string url = @"";
HtmlDocument doc = hw.Load(url);
HtmlNode bodyNode = doc.DocumentNode.SelectSingleNode("html/body");
HtmlNode newTag = HtmlNode.CreateNode("<h1>\"a\"</h1");
doc.DocumentNode.InsertAfter(newTag, node);

When I run the code the application fails in the line doc.DocumentNode.InsertAfter, the following is the exception I receive:
Is possible what I'm trying to do, looks like is supported, What I'm doing wrong.
Thanks in advanced

Unhandled Exception: System.ArgumentOutOfRangeException: Node "<body class="ltr"
></body>" was not found in the collection
Parameter name: node
   at HtmlAgilityPack.HtmlNodeCollection.get_Item(HtmlNode node) in C:\Documents
 and Settings\xx\Desktop\HTMLParser\HtmlAgilityPack20\HtmlAgilityPack\HtmlNodeCo
llection.cs:line 250
   at HtmlAgilityPack.HtmlNode.InsertAfter(HtmlNode newChild, HtmlNode refChild)
 in C:\Documents and Settings\xx\Desktop\HTMLParser\HtmlAgilityPack20\HtmlAgilit
yPack\HtmlNode.cs:line 981
   at HtmlAgilityPack.Samples.GetDocLinks.Main(String[] args) in C:\Documents an
d Settings\xx\Desktop\HTMLParser\HtmlAgilityPack20\GetDocLinks\GetDocLinks.cs:li
ne 16
Press any key to continue . . .