DocumentElement.SelectNodes returns null (Html Agility Pack)

c# html-agility-pack

Question

I just need to parse through a table in html, which looks like:

<html>
<head>
  <title>
  </title>
</head>
<body>
  <table>
  ***contents***
  </table>
</body>
</html>

My code looks like this:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(@path);
foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table"))
{
  foreach (HtmlNode row in table.SelectNodes("tr"))
  {
    foreach (HtmlNode cell in row.SelectNodes("th|td"))
    {
      ***copy content***
    }
  }
}

But then I get NullReferenceException at

foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table"))

So table doesn't have anything in it, but why?

Accepted Answer

I've fixed it. The problem was the LoadHtml method, which takes an html string as a parameter. For a file Load should be used.


Popular Answer

As you dont have any tr's table rows in your HTML the agility pack is not finding any

 try this 
            // Get all tables in the document
            HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table");

            // Iterate all rows in the first table
            HtmlNodeCollection rows = tables[0].SelectNodes(".//tr");
            for (int i = 0; i < rows.Count; ++i)
            {


            }



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