user3107806
user3107806

Reputation: 29

SelectNode Return Null in Html Agility Pack

I saw a lots of similar posts, both of them talk about SelectSingleNode return null. I'm not quite sure my problem was related to that. Perhaps I had some problems which I could not figure out. Here my codes :

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");
}

Upvotes: 0

Views: 1050

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236248

If there is no <div> tags with class equal to content, then nothing is found and you have null. That's by design.

UPDATE: You are not loading data into HtmlDocument. You have doc instance which is not related for data you are loading. Use document which is returned by Load method:

HtmlDocument doc = web.Load(url);

Upvotes: 1

Related Questions