0

I have this xml file:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!--This file represents the results of running a test suite-->
<test-results name="<path>" total="1" errors="0" failures="0" not-run="0" inconclusive="0" ignored="0" skipped="0" invalid="0" date="2014-08-12" time="16:05:41">
  <environment nunit-version="2.6.3.13283" (...)
(...)
</test-results>

I want to get value of total, errors to integers in program "total", "errors" and so on. How can I get this values?

Sowiarz
  • 1,071
  • 2
  • 13
  • 27
  • [Parsing XML (MSDN)](http://msdn.microsoft.com/en-gb/library/bb387059.aspx) – Sayse Aug 13 '14 at 06:20
  • You didn't post your c# code – Mike Cheel Aug 13 '14 at 06:21
  • I tried methods which were used in topic "Hod does one parese XML files?" but I had problem with open xml (something was wrong with structure of xml). And I think it is not duplicate. This question is about get values to int, not about main problem of parsing XML. Sorry for no c# code but I was searching for any good ideas to solve this problem. – Sowiarz Aug 13 '14 at 07:11

3 Answers3

1

Try below code :-

 var xDoc = XDocument.Load("XMLFile1.xml");

                foreach (var elem in xDoc.Document.Descendants("test-results"))
                {
                    var total = int.Parse(elem.Attribute("total").Value);
                    var error = int.Parse(elem.Attribute("errors").Value);
                }
Neel
  • 11,625
  • 3
  • 43
  • 61
1

thry this

        XDocument xDoc = XDocument.Load(@"your path");
        int total=0;


        foreach (var elem in xDoc.Document.Descendants("test-results"))
        {
            total += int.Parse(elem.Attribute("total").Value);

        }
Amjad Abdelrahman
  • 3,372
  • 1
  • 13
  • 20
0

Use following:

//path is the path to your xml file
XmlTextReader reader = new XmlTextReader(path);

XmlDocument doc = new XmlDocument();
XmlNode node = doc.ReadNode(reader);

foreach (XmlNode chldNode in node.ChildNodes)
{
    //Read the attribute Name
    if (chldNode.Name.ToString().Equals("test-results"))
    {                    
        int total = Int32.Parse(chldNode.Attributes["total"].Value.ToString());
    }
}
HaMi
  • 539
  • 6
  • 23