【发布时间】:2013-05-30 07:51:58
【问题描述】:
当html文件中有复合表时,如何计算父表的行数。
我所说的复合表是什么意思;一个表格,其中其他表格包含在其某些单元格中。
这是我的编码尝试。注意我收到的值不正确:
String htmlFile = "C:/Temp/Test_13.html";
HtmlDocument doc = new HtmlDocument();
doc.Load(htmlFile);
HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table");
HtmlNodeCollection rows = tables[1].SelectNodes(".//tr");
Console.WriteLine(" Rows in second (Parent) table: " + rows.Count());
请说明您的答案中使用了哪个命名空间。
这是一个有代表性的示例文件:
<html>
<body>
<table border="1">
<tr>
<td>Apps</td>
</tr>
<tr>
<td>Offcie Web Apps</td>
</tr>
</table>
<br/>
<table border="1">
<tr>
<td>Application</td>
<td>Status</td>
<td>Instances</td>
</tr>
<tr>
<td>PowerPoint</td>
<td>Online</td>
<td>
<table border="1">
<tr>
<td>Server1</td>
<td>Online</td>
</tr>
<tr>
<td>Server2</td>
<td>Disabled</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>Word</td>
<td>Online</td>
<td>
<table border="1">
<tr>
<td>Server1</td>
<td>Online</td>
</tr>
<tr>
<td>Server2</td>
<td>Disabled</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
谢谢。
【问题讨论】:
-
您使用的是 Visual Studio,对吗?通过在调试期间将鼠标悬停在它们上来检查您的
tables和rows集合;看看里面有什么。
标签: c# linq html-parsing html-agility-pack