Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Html敏捷包中的SelectNode返回Null_C#_Html_Html Agility Pack - Fatal编程技术网

C# Html敏捷包中的SelectNode返回Null

C# Html敏捷包中的SelectNode返回Null,c#,html,html-agility-pack,C#,Html,Html Agility Pack,我看到很多类似的帖子,它们都在谈论SelectSingleNodeReturnNull。我不太确定我的问题是否与此有关。也许我有一些我想不出来的问题。这是我的密码: string url = "https://www.google.com/#q=nothing"; HtmlWeb web = new HtmlWeb(); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); web.Load(url)

我看到很多类似的帖子,它们都在谈论SelectSingleNodeReturnNull。我不太确定我的问题是否与此有关。也许我有一些我想不出来的问题。这是我的密码:

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");
}
如果没有class等于
content
标记,则找不到任何内容,您的
为null
。那是故意的

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

HtmlDocument doc = web.Load(url);