从 XML 中检索属于特定节点的节点 [英] Retrieve nodes from XML belonging to a specific node
本文介绍了从 XML 中检索属于特定节点的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的 XML:
I have a XML like this:
<ITEM>
<RACES>
<TAB>
<NUMBER>1</NUMBER>
<A></A>
<B></B>
</TAB>
<TAB>
<NUMBER>2</NUMBER>
<A></A>
<B></B>
</TAB>
</RACES>
</ITEM>
是否可以将所有仅属于 TAB 且编号为 1 的 As 和 Bs 节点检索为 XmlNodeList?
is it possible to retrieve as XmlNodeList all the As and Bs nodes that belong to only TAB with NUMBER 1?
我使用以下代码,但它当然给了我 2 个节点.我只想要 1 个节点:
I use the following codes, but it gives me of course 2 nodes. I want only 1 node :
XmlNodeList xnList = xml.SelectNodes("/ITEM/RACES/TAB/A");
推荐答案
你可以做 xmlDocument.SelectNodes(expression)
You can do xmlDocument.SelectNodes(expression)
如果您需要两个节点 A &乙
where if you need both nodes A & B
expression = @"//TAB[NUMBER=1]/A|//TAB[NUMBER=1]/B"
如果你只需要单独的A节点
if you need only A node seperately
expression = @"//TAB[NUMBER=1]/A"
如果你只需要单独的B节点
if you need only B node seperately
expression = @"//TAB[NUMBER=1]/B"
这篇关于从 XML 中检索属于特定节点的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文