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?

有帮助吗?

解决方案 2

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.

其他提示

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)
            {


            }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top