我有一个由HtmlAgilityPack编写的程序,它不能正常工作。Url和节点是正确的。但是它有一个错误,它指的是预言。
错误是
对象引用未设置为对象的实例。
以前起过作用。我删除HtmlAgilityPack dll并再次添加它。但不起作用。我需要访问标签的href。
我的代码是:
string source = wc.DownloadString("http://example.com");
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(source);
foreach (HtmlNode div in
document.DocumentNode.SelectNodes("//div[@class='test']/a"))
{
//do something
}发布于 2014-04-16 06:26:36
对于Ulugbek Umirov,下面是带有空引用检查的代码:
string source = wc.DownloadString("http://example.com");
HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(source);
var documentNode = document.DocumentNode;
if ( documentNode!=null )
{
var nodes = documentNode.SelectNodes("//div[@class='test']/a");
if ( nodes!=null )
{
foreach (HtmlNode div in nodes)
{
// Do something...
}
}
}https://stackoverflow.com/questions/23101449
复制相似问题