在C#中解析XML并搜索“失败”

时间:2014-05-05 14:43:00

标签: c# xml parsing xml-parsing searching-xml

我正在尝试用C#编写一个程序来解析XML文件,并查看多个元素的状态以找到一个失败的程序。我没有多少使用XML的经验,但已经做了一些研究而且有点困难。我将在下面发布一个示例XML文件以供参考。我想我会首先检查每个组中的哪个组,然后查看哪个组失败,然后搜索该组的各个元素。

<Diagnostics ActionType="SPV" Ver="48">
  <StartTimestamp>2014-04-18 13:36:44Z</StartTimestamp> 
- <Iteration>
- <NODE1>
     <Device Name="A" Register="IDCODE" Status="PASSED"/> 
     <Device Name="B" Register="IDCODE" Status="PASSED"/> 
     <Device Name="C" Register="IDCODE" Status="PASSED"/> 
     <Device Name="D" Register="IDCODE" Status="PASSED"/> 
     <Device Name="E" Register="IDCODE" Status="PASSED"/> 
     <Device Name="F" Register="IDCODE" Status="PASSED"/> 
     <Device Name="G" Register="IDCODE" Status="PASSED"/> 
     <Device Name="H" Register="IDCODE" Status="PASSED"/> 
     <Device Name="I" Register="IDCODE" Status="PASSED"/> 
     <Status>PASSED</Status> 
  </NODE1>
- <NODE2>
     <Status>PASSED</Status> 
  </NODE2>
- <NODE3>
     <Status>PASSED</Status> 
  </NODE3>
- <NODE4>
     <Device Name="A" Register="IDCODE" Status="PASSED"/> 
     <Device Name="B" Register="IDCODE" Status="PASSED"/> 
     <Device Name="C" Register="IDCODE" Status="PASSED"/> 
     <Device Name="D" Register="IDCODE" Status="PASSED"/> 
     <Device Name="E" Register="IDCODE" Status="PASSED"/> 
     <Device Name="F" Register="IDCODE" Status="PASSED"/> 
     <Device Name="G" Register="IDCODE" Status="PASSED"/> 
     <Device Name="H" Register="IDCODE" Status="PASSED"/> 
     <Device Name="I" Register="IDCODE" Status="PASSED"/> 
     <Status>PASSED</Status> 
  </NODE4>
- <NODE5>
     <Device Name="E" Status="PASSED" /> 
     <Device Name="F" Status="PASSED" /> 
     <Device Name="H" Status="FAILED" /> 
     <Device Name="I" Status="PASSED" /> 
     <Status>FAILED</Status> 
  </NODE5>
  <Passed>False</Passed> 
 </Iteration>
 <EndTimestamp>2014-04-18 13:36:44Z</EndTimestamp> 
</Diagnostics>

1 个答案:

答案 0 :(得分:1)

XmlNodeList nodes = xmlDoc.SelectNodes(“// Device [@ Status ='FAILED']”);