Question

I am trying to write a program in C# to parse through an XML file and look through the status of multiple elements to find one that failed. I don't have much experience working with XML but have done some research and am a little stuck. I'll post an example XML file below for reference. I figure I'll check the of each group first to see which group failed then search that group's individual elements.

<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>
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top