我喜欢XML
...
<S head="X">
<A head="X">
<A1 head="Z">
<A2 head="X">
<B head="Y">
</S>
...
我想选择S
的子项,其子项的值为“X”。 (注意我对元素S
,A
,B
...... )的名称一无所知。
我试过
string headTag = node.SelectSingleNode("//*[@head='X']").Name;
它会返回“S”,而如果node
指向S
,我希望得到“A”,如果node
指向A
,我希望得到“A”。
答案 0 :(得分:3)
此XPath将选择所有子元素,无论名称如何,@head
属性值等于X
:
./*[@head='X']
从当前节点开始。
当前节点为S
时,它会选择A
;当前节点为A
时,它会根据请求选择A2
。