我的智慧结束了。每当我尝试使用XPath选择节点时,我一直在使用HtmlAgilityPack获得此异常。即使我只用这个例子开始一个全新的解决方案,问题仍然存在(因此我的应用程序代码不会以某种方式干扰问题。网页没有任何问题,或者我的网络连接或类似的东西。我知道我有过以前这个工作。我甚至怀疑一个损坏的dll并重新下载它,但无济于事。任何想法?
using System;
using HtmlAgilityPack;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var web = new HtmlWeb();
var doc = web.Load("http://www.google.com");
var root = doc.DocumentNode;
var links = root.SelectNodes("//a");
// Error! ArgumentOutOfRangeException: Index was out of range.
// Must be non-negative and less than the size of the collection.
}
}
}
/*
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at HtmlAgilityPack.HtmlNodeNavigator.MoveToFirstChild()
at MS.Internal.Xml.XPath.XPathDescendantIterator.MoveNext()
at MS.Internal.Xml.XPath.DescendantQuery.Advance()
at MS.Internal.Xml.XPath.XPathSelectionIterator.MoveNext()
at HtmlAgilityPack.HtmlNode.SelectNodes(String xpath)
at ConsoleApplication1.Program.Main(String[] args)
*/
编辑:
嗯......我不确定它是怎么发生的或它做了什么,但我发现.dll安装在GAC_MSIL文件夹中。删除它立即解决了问题。所以,没关系!