在特殊的 xml 结构中选择具有 name 属性 name 的元素

问题描述:

下面是我的 xml 文档的结构.我只想先取每个节点的值 然后将它与给定的值进行比较.但是我不知道如何在 c# 中使用 xml selectnodes 定位每个节点的 <attribute name="a"> .谷歌搜索没有显示任何有效的解决方案.

below is the structure of my xml document. I just want to first take the value of every node <attribute name="a"> then make a comparison of it with a given value. However I don't how to locate <attribute name="a"> of each node using xml selectnodes in c#. Google searches don't show any working solutions.

<nodes>     
 <node name = "node1">      
  <attribute name="a">This is node1 a</attribute>
  <attribute name="b">This is node1 b</attribute>
 </node>
 <node name = "node2">      
  <attribute name="a">This is node2 a</attribute>
  <attribute name="b">This is node2 b</attribute>
 </node>
 ...
</nodes>     

假设您问题中的 XML 标记代表您的整个文档,您可以这样做:

Assuming the XML markup in your question represents your whole document, you can do:

XmlNodeList attrElements
    = yourDocument.SelectNodes("/nodes/node/attribute[@name='a']");