【发布时间】:2012-04-12 15:33:42
【问题描述】:
我正在尝试解析下表,但不幸的是每个节点似乎都相互嵌套。 :( 获取子节点是不可能的,因为它总是给出 count = 1
这真的很有趣,但它正在寻找;例如下一个“tr”作为前一个 tr 的子节点?
你有什么想法吗?
<table width="292px" border="0">
<tr>
<td>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td colspan="2" bgcolor="#FBCE9D" align="center" height="40">
</td>
</tr>
<tr>
<td bgcolor="#FFF4D2" height="25" width="60">
</td>
<td height="25" bgcolor="#e8e8e8">
</td>
</tr>
<tr>
<td bgcolor="#FFF4D2" height="25" width="60">
</td>
<td height="25" bgcolor="#e8e8e8">
</td>
</tr>
<tr>
<td bgcolor="#FFF4D2" height="25" width="60">
</td>
<td height="25" bgcolor="#e8e8e8">
</td>
</tr>
<tr>
<td bgcolor="#FFF4D2" height="25" width="60">
</td>
<td height="25" bgcolor="#e8e8e8">
</td> //Here is a missing "</tr>" and I think this one is confusing the agilitypack!
<tr>
<td bgcolor="#FFF4D2" height="35" colspan="2" align="center">
</td>
</tr>
</table>
</td>
</tr>
</table>
我的代码是:
var webGet = new HtmlWeb();
var doc = webGet.Load("the url where this table is located");
HtmlNodeCollection tb = doc.DocumentNode.SelectNodes("//table[@width='292px']");
var table = tb[0].ChildNodes[1].ChildNodes[0].ChildNodes[0].ChildNodes;
for (var na = 0; na < table.Count; na++)
{ .....do the work.... }
实际上,这段代码之前的工作就像一个魅力,但他们在里面嵌套了另一个表,上面贴着ChildNodes[1],因为不会有ChildNodes[1],它总是ChildNodes[0]?
还有一个注意事项; Firebug 将“/html/body/table/tbody/tr[2]/td/table/tbody”显示为嵌套表的 XPath,但您可能注意到“tbody”不熟悉 htmlagility,因为它是由浏览器删除缺少的关闭标签 /tr
【问题讨论】:
-
你的代码是什么样的?
-
对不起,我忘了。你可以在上面找到它。
标签: html-parsing html-agility-pack