XPath-如何选择节点的子元素? [英] XPath - how do you select the child elements of a node?
本文介绍了XPath-如何选择节点的子元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含XHTML表的XmlDocument.我想遍历它一次一次处理表单元格,但是下面的代码返回嵌套循环中的所有单元格,而不仅仅是返回当前行的所有单元格:
I have an XmlDocument containing a XHTML table. I'd like to loop through it to process the table cells one row at a time, but the code below is returning all the cells in the nested loop instead of just those for the current row:
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
}
}
我做错了什么?谢谢
推荐答案
以."开头的内部路径.表示您要从当前节点开始.开头的"/" 始终从xml文档的根目录开始搜索,即使您在子节点上指定了它也是如此.
Start the inner path with a "." to signal that you want to start at the current node. A starting "/" always searches from the root of the xml document, even if you specify it on a subnode.
所以:
XmlNodeList tableCells = tableRow.SelectNodes(".//td");
甚至
XmlNodeList tableCells = tableRow.SelectNodes("./td");
,因为那些< td>
可能直接在该< tr>
之下.
as those <td>
s probably are directly under that <tr>
.
这篇关于XPath-如何选择节点的子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文