Html agility pack not loading url

c# html-agility-pack html-parsing

Question

I have something like this:

class MyTask
{
    public MyTask(int id)
    {
        Id = id;
        IsBusy = false;
        Document = new HtmlDocument();
    }

    public HtmlDocument Document { get; set; }
    public int Id { get; set; }
    public bool IsBusy { get; set; }
}

class Program
{
    public static void Main()
    {
        var task = new MyTask(1);
        task.Document.LoadHtml("http://urltomysite");
        if (task.Document.DocumentNode.SelectNodes("//span[@class='some-class']").Count == 0)
        {
            task.IsBusy = false;
            return;
        }   
    }
}

Now when I start my program, it throws an error on the if sttement, saying that Object reference not set to an instance of an object.. Why isn't it loading my page? What am I doing wrong here?

Accepted Answer

You are looking for .Load().

.LoadHtml() expects to be given physical HTML. You are giving a website to go to:

HtmlWeb website = new HtmlWeb();
HtmlDocument rootDocument = website.Load("http://www.example.com");

Popular Answer

In addition to Arran's answer

If .SelectNodes("//span[@class='some-class']") doesn't return any nodes and is null then doing a Count on it will give this exception.

Try

if (task.Document.DocumentNode.SelectNodes("//span[@class='some-class']") != null && 
    task.Document.DocumentNode.SelectNodes("//span[@class='some-class']").Count == 0)
    {
        task.IsBusy = false;
        return;
    }   


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why