2013-12-18 70 views
0

我看到很多类似的帖子,他们都谈论SelectSingleNode返回null。我不太确定我的问题与此有关。也许我有一些我无法弄清楚的问题。在这里我的代码:SelectNode在Html敏捷包中返回Null

string url = "https://www.google.com/#q=nothing";  
HtmlWeb web = new HtmlWeb(); 
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 
web.Load(url); 
var nodes = doc.DocumentNode.SelectNodes("//div[@class='content']"); 
if (nodes != null) { 
    foreach(HtmlNode item in nodes) { 
     if (item != null) { 
      string s = item.InnerText; 
      listView1.Items.Add(s); 
     } 
    } 
} else { 
    MessageBox.Show("Nothing found here"); 
} 

回答

1

如果没有<div>标签带班等于content,则没有被发现,你有null。这是设计。

更新:您尚未将数据加载到HtmlDocument。您的doc实例与您正在加载的数据无关。使用由Load返回的文件方法:

HtmlDocument doc = web.Load(url);