在下面的HTML中,我可以解析table
元素,但我不知道如何跳過th
元素。解析表與HTML敏捷包
我想只有td
元素,但是當我嘗試使用:
foreach (HtmlNode cell in row.SelectNodes("td"))
...我得到一個異常。
<table class="tab03">
<tbody>
<tr>
<th class="right" rowspan="2">first</th>
</tr>
<tr>
<th class="right">lp</th>
<th class="right">name</th>
</tr>
<tr>
<td class="right">1</td>
<td class="left">house</td>
</tr>
<tr>
<th class="right" rowspan="2">Second</th>
</tr>
<tr>
<td class="right">2</td>
<td class="left">door</td>
</tr>
</tbody>
</table>
我的代碼:
var document = doc.DocumentNode.SelectNodes("//table");
string store = "";
if (document != null)
{
foreach (HtmlNode table in document)
{
if (table != null)
{
foreach (HtmlNode row in table.SelectNodes("tr"))
{
store = "";
foreach (HtmlNode cell in row.SelectNodes("th|td"))
{
store = store + cell.InnerText+"|";
}
sw.Write(store);
sw.WriteLine();
}
}
}
}
sw.Flush();
sw.Close();
什麼異常? –