Setr Setr - 1 month ago 10
C# Question

C# Html parsing HtmlAgilityPack not working

I am trying to read html table in order to read and change table with using HtmlAgilityPack. I tried with very different websites but my code does not work.


SelectSingleNode function returns null. Also when I call SelectNodes instead of SelectSingleNode, result is null again.


Example link
http://www.uefa.com/livescores/ has a table. How can I read this table?

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml("http://www.uefa.com/livescores/");
var result = doc.DocumentNode.SelectSingleNode("//table");

Mun Mun
Answer

You're using the LoadHtml method which is used to load a HTML string, not load HTML from a URL. However, the Load method does not appear to support loading content from a URL and you may need to download the HTML separately and then load it it into the HtmlDocument class to use it.

For example:

var url = "http://www.uefa.com/livescores/";
var content = new System.Net.WebClient().DownloadString(url);

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(content);
var hn = doc.DocumentNode.SelectSingleNode("//table");

You can also use the HtmlWeb utility class:

var web = new HtmlWeb();
var doc = web.Load(url);
var hn = doc.DocumentNode.SelectSingleNode("//table");
Comments