我有一个包含 XHTML 表的 XmlDocument。我想循环遍历它以一次处理一行表格单元格,但下面的代码返回嵌套循环中的所有单元格,而不仅仅是当前行的单元格:
XmlNodeList tableRows = xdoc.SelectNodes("//tr");
foreach (XmlElement tableRow in tableRows)
{
XmlNodeList tableCells = tableRow.SelectNodes("//td");
foreach (XmlElement tableCell in tableCells)
{
// this loops through all the table cells in the XmlDocument,
// instead of just the table cells in the current row
}
}
我究竟做错了什么?谢谢
内部路径以“.”开始。表示您要从当前节点开始。以“/”开头always即使您在子节点上指定它,也会从 xml 文档的根开始搜索。
So:
XmlNodeList tableCells = tableRow.SelectNodes(".//td");
or even
XmlNodeList tableCells = tableRow.SelectNodes("./td");
作为那些<td>
s 可能直接在那个之下<tr>
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)