Setr Setr - 1 year ago 172
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 has a table. How can I read this table?

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
var result = doc.DocumentNode.SelectSingleNode("//table");

Mun Mun
Answer Source

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 = "";
var content = new System.Net.WebClient().DownloadString(url);

var doc = new HtmlAgilityPack.HtmlDocument();
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");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download