Html敏捷包不加载url

我有这样的事情:

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

现在当我启动我的程序时,它会在if文件上抛出一个错误,说Object reference not set to an instance of an object. 。 为什么不加载我的页面? 我在这做错了什么?

您正在寻找.Load()

.LoadHtml()期望获得物理HTML。 您正在提供一个网站:

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

除了Arran的回答

如果.SelectNodes("//span[@class='some-class']")没有返回任何节点并且为null那么对它执行Count会产生此exception。

尝试

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