Search code examples
c#xmlxmldocument

Unable to access inner Xml elements


Question Background:

I have extracted the following inner XML from a larger document:

<Counters total="1" executed="1" passed="1" error="0" failed="0" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="0" notExecuted="0" disconnected="0" warning="0" completed="0" inProgress="0" pending="0" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" /> 

Issue:

Using the following code, I have attempted to access each of the elements of the above XML. I need to extract both the name i.e 'total' and its value '1';

 XmlDocument innerXmlDoc = new XmlDocument();

 innerXmlDoc.LoadXml(node.InnerXml);

 XmlElement element = innerXmlDoc.DocumentElement;

 XmlNodeList elements = element.ChildNodes;

 for (int i = 0; i < elements.Count; i++)
 {
     //logic
 }

If someone can tell me how to get these values that would be great.


Solution

  • Managed to solve this myself:

     foreach (XmlNode node in nodes) 
     {
          XmlDocument innerXmlDoc = new XmlDocument();
    
          innerXmlDoc.LoadXml(node.InnerXml);
    
          var list  = innerXmlDoc.GetElementsByTagName("Counters");
    
          for (int i = 0; i < list.Count; i++)
          {
             string val = list[i].Attributes["total"].Value;
          }
     };