HTML敏捷性包 - 通过行和列循环 [英] Html Agility Pack - loop through rows and columns
问题描述
如何通过表和行有一个属性ID或名称深得到内部文本中的每个TD手机下来循环?我在asp.net,C#和最新的HTML敏捷包工作。请指导。谢谢你。
这是HTML文件有好几桌。他们中的一个具有属性编号=主要部分。在该确定的表中,有许多行。其中有些行具有相同的属性名称=显示屏。在这些命名行,有我不得不从文本中提取多个列。事情是这样的:
<身体GT;
<表>
...
< /表>
<表>
...
< /表><表ID =主要部分>
&所述; TR>
< TD>< / TD>
...
< / TR>
&所述; TR>
< TD>< / TD>
...
< / TR>
< TR NAME =显示>
< TD> 1月及LT; / TD>
< TD>与二月LT; / TD>
< TD> MAR< / TD>
...
< / TR>
< TR NAME =显示>
< TD>&四月LT; / TD>
< TD>&五月LT; / TD>
< TD> 6月< / TD>
...
< / TR>
< TR NAME =显示>
< TD>七月及LT; / TD>
< TD>日及LT; / TD>
< TD>与九月LT; / TD>
...
< / TR>
&所述; TR>
< TD>< / TD>
...
< / TR>
< TR NAME =显示>
< TD> 10月< / TD>
< TD>&十一月LT; / TD>
< TD>十二月< / TD>
...
< / TR>
&所述; TR>
< TD>< / TD>
...
< / TR>
< /表>
<表>
...
< /表>
< /身体GT;
您需要使用XPath来选择这些节点:
在doc.DocumentElement.SelectNodes(的foreach(HtmlNode细胞// TR [@名称='显示'] / TD)
{
//获取单元格数据
}
How can I loop through table and row that have an attribute id or name to get inner text in deep down in each td cell? I work on asp.net, c#, and the newest html agility package. Please guide. Thank you.
An html file have several tables. One of them has an attribute id=main-part. In that identified table, there are many rows. Some of those rows have same attribute name=display. In those named rows, there are many columns which I have to extract text from. Something like this:
<body>
<table>
...
</table>
<table>
...
</table>
<table id="main-part">
<tr>
<td></td>
...
</tr>
<tr>
<td></td>
...
</tr>
<tr name="display">
<td>Jan</td>
<td>Feb</td>
<td>Mar</td>
...
</tr>
<tr name="display">
<td>Apr</td>
<td>May</td>
<td>June</td>
...
</tr>
<tr name="display">
<td>Jul</td>
<td>Aug</td>
<td>Sep</td>
...
</tr>
<tr>
<td></td>
...
</tr>
<tr name="display">
<td>Oct</td>
<td>Nov</td>
<td>Dec</td>
...
</tr>
<tr>
<td></td>
...
</tr>
</table>
<table>
...
</table>
</body>
You need to select these nodes using xpath:
foreach(HtmlNode cell in doc.DocumentElement.SelectNodes("//tr[@name='display']/td")
{
// get cell data
}
这篇关于HTML敏捷性包 - 通过行和列循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!