gpt4 book ai didi

c# - 如何计算html文件C#中表格中的行数

转载 作者:行者123 更新时间:2023-11-30 17:04:57 24 4
gpt4 key购买 nike

当 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>

谢谢。

最佳答案

您可以推送每个 <table><tr>到堆栈,当你遇到 </table> - 弹出直到表格从堆栈中弹出。

关于c# - 如何计算html文件C#中表格中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16909002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com