ArgumentOutOfRangeException in HtmlNode

Topics: Developer Forum
Dec 14, 2011 at 6:02 AM

ArgumentOutOfRangeException in HtmlNode for version 1.4.0.0 It's not often but it suddenly threw an exception

 

STATEMENT ON MY APP
HtmlNodeCollection nodes = htmldoc.DocumentNode.SelectNodes("//div[@class='winbody']/ul/li/p[@class='postmeta']/a[contains(@href,'ratings.php')]");

THROWN AN ArgumentOutOfRangeException ERROR
System.ArgumentOutOfRangeException was unhandled by user code
  Message="startIndex cannot be larger than length of string.\r\nParameter name: startIndex"
  Source="mscorlib"
  ParamName="startIndex"
  StackTrace:
       at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
       at System.String.Substring(Int32 startIndex, Int32 length)
       at HtmlAgilityPack.HtmlNode.get_OuterHtml() in D:\Code Repository\Open Source\Html Agility Pack 2\Release\1_4_0\HtmlAgilityPack\HtmlNode.cs:line 503
       at HtmlAgilityPack.HtmlTextNode.get_Text() in D:\Code Repository\Open Source\Html Agility Pack 2\Release\1_4_0\HtmlAgilityPack\HtmlTextNode.cs:line 60
       at HtmlAgilityPack.HtmlNode.CloneNode(Boolean deep) in D:\Code Repository\Open Source\Html Agility Pack 2\Release\1_4_0\HtmlAgilityPack\HtmlNode.cs:line 853
       at HtmlAgilityPack.HtmlNodeNavigator.MoveToNext() in D:\Code Repository\Open Source\Html Agility Pack 2\Release\1_4_0\HtmlAgilityPack\HtmlNodeNavigator.cs:line 636
       at System.Xml.XPath.XPathNavigator.CompareSiblings(XPathNavigator n1, XPathNavigator n2)
       at System.Xml.XPath.XPathNavigator.ComparePosition(XPathNavigator nav)
       at MS.Internal.Xml.XPath.Query.CompareNodes(XPathNavigator l, XPathNavigator r)
       at MS.Internal.Xml.XPath.CacheChildrenQuery.DecideNextNode()
       at MS.Internal.Xml.XPath.CacheChildrenQuery.Advance()
       at MS.Internal.Xml.XPath.FilterQuery.Advance()
       at MS.Internal.Xml.XPath.CacheChildrenQuery.GetNextInput()
       at MS.Internal.Xml.XPath.CacheChildrenQuery.DecideNextNode()
       at MS.Internal.Xml.XPath.CacheChildrenQuery.Advance()
       at MS.Internal.Xml.XPath.FilterQuery.Advance()
       at MS.Internal.Xml.XPath.XPathSelectionIterator.MoveNext()
       at HtmlAgilityPack.HtmlNode.SelectNodes(String xpath) in D:\Code Repository\Open Source\Html Agility Pack 2\Release\1_4_0\HtmlAgilityPack\HtmlNode.cs:line 1425
       at TipidUpUI.MainWindow.SayUp(String iid) in E:\Codes\TipidUp\TipidUpUI\MainWindow.cs:line 445
       at TipidUpUI.MainWindow.bgWorker_DoWork(Object sender, DoWorkEventArgs e) in E:\Codes\TipidUp\TipidUpUI\MainWindow.cs:line 280
       at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
  InnerException:


CALL STACK
     [External Code]   
>    HtmlAgilityPack.dll!HtmlAgilityPack.HtmlNode.OuterHtml.get() Line 503 + 0x28 bytes    C#
     HtmlAgilityPack.dll!HtmlAgilityPack.HtmlTextNode.Text.get() Line 60 + 0x8 bytes    C#
     HtmlAgilityPack.dll!HtmlAgilityPack.HtmlNode.CloneNode(bool deep = false) Line 853 + 0x5d bytes    C#
     HtmlAgilityPack.dll!HtmlAgilityPack.HtmlNodeNavigator.MoveToNext() Line 636 + 0x2b bytes    C#
     [External Code]   
     HtmlAgilityPack.dll!HtmlAgilityPack.HtmlNode.SelectNodes(string xpath = "//div[@class='winbody']/ul/li/p[@class='postmeta']/a[contains(@href,'ratings.php')]") Line 1425 + 0x8 bytes    C#
     TipidUpUI.exe!TipidUpUI.MainWindow.SayUp(string iid = "3245422") Line 445 + 0x29 bytes    C#
     TipidUpUI.exe!TipidUpUI.MainWindow.bgWorker_DoWork(object sender = {System.ComponentModel.BackgroundWorker}, System.ComponentModel.DoWorkEventArgs e = {System.ComponentModel.DoWorkEventArgs}) Line 280 + 0xb bytes    C#
     [External Code]   

STATEMENT in HtmlAgilityPack.HtmlNode @line number 503
return _ownerdocument._text.Substring(_outerstartindex, _outerlength);

_outerstartindex=74947
_outerlength = 17
_ownerdocument._text = check this link http://pastebin.com/TrGPbVqr

Mar 11, 2012 at 9:37 PM

That's worked here, no more exception:

                    //Just empty the doc object                   
                    doc = new HtmlAgilityPack.HtmlDocument();
                    doc.LoadHtml(rows[i].OuterHtml);                   
                    HtmlAgilityPack.HtmlNodeCollection cells = doc.DocumentNode.SelectNodes("//td");