1

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");
1
  • but my code does not work What does not work? What errors do you see? Describe what is happening. Commented Mar 17, 2016 at 17:32

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

3 Comments

If output of parsing is wrong, is there anything to fix? Because mine result is not true. All of the </td> </tr> are at end of result. I could not share my actual web page. But result like below <td><a href=test>test</a> <td alıgn="right">7 <tr><td><a href=test">test</a> <td><a href="a.html">test</a> <td alıgn="right">5 .... </td></td></tr></td></td></td></tr></td></td></td></tr></td></td></td>
If you have a valid HTML document, you can use any XPath statement to select the markup that you need.
There is a hr tag inside html, so xpath also gives error. When I remove hr tags manually it fixes. Because hr does not have any closing like </hr> Am i wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.