在c#中解析Report.xml

本文关键字:Report xml | 更新日期: 2023-09-27 17:54:08

我明白这个问题可以是一个通用的问题,我想解析下面的xml结构。我只对testcase标签的属性名称和时间感兴趣,如果它有失败的子消息属性,我可以单独做,即所有的测试用例名称和时间在一个镜头中,类似地,所有的失败消息分开。但我从来没有能够保持打印名称和时间跟着任何失败的消息,如果任何连续。

<?xml version="1.0" encoding="UTF-8"?>
    <testsuites tests="4" failures="2" disabled="0" errors="0" timestamp="2014-03-27T07:58:04" time="0.168" name="AllTests">
      <testsuite name="FactorialTest" tests="3" failures="2" disabled="0" errors="0" time="0.139">
        <testcase name="Negative" status="run" time="0.047" classname="FactorialTest">
          <failure message="c:'users'win_7_32_sp1'documents'visual studio 2012'projects'simplemath'unit_test_simplemath'unit_test_simplemath.cpp:13&#x0A;Value of: Factorial(-1)&#x0A;  Actual: -1&#x0A;Expected: -10" type=""><![CDATA[c:'users'win_7_32_sp1'documents'visual studio 2012'projects'simplemath'unit_test_simplemath'unit_test_simplemath.cpp:13
    Value of: Factorial(-1)
      Actual: -1
    Expected: -10]]></failure>
        </testcase>
        <testcase name="Zero" status="run" time="0.001" classname="FactorialTest" />
        <testcase name="Positive" status="run" time="0.071" classname="FactorialTest">
          <failure message="c:'users'win_7_32_sp1'documents'visual studio 2012'projects'simplemath'unit_test_simplemath'unit_test_simplemath.cpp:24&#x0A;Value of: Factorial(1)&#x0A;  Actual: 1&#x0A;Expected: 11" type=""><![CDATA[c:'users'win_7_32_sp1'documents'visual studio 2012'projects'simplemath'unit_test_simplemath'unit_test_simplemath.cpp:24
    Value of: Factorial(1)
      Actual: 1
    Expected: 11]]></failure>
          <failure message="c:'users'win_7_32_sp1'documents'visual studio 2012'projects'simplemath'unit_test_simplemath'unit_test_simplemath.cpp:25&#x0A;Value of: Factorial(2)&#x0A;  Actual: 2&#x0A;Expected: 22" type=""><![CDATA[c:'users'win_7_32_sp1'documents'visual studio 2012'projects'simplemath'unit_test_simplemath'unit_test_simplemath.cpp:25
    Value of: Factorial(2)
      Actual: 2
    Expected: 22]]></failure>
        </testcase>
      </testsuite>
      <testsuite name="TestSuiteName" tests="1" failures="0" disabled="0" errors="0" time="0.004">
        <testcase name="Suite2" status="run" time="0" classname="TestSuiteName" />
      </testsuite>
    </testsuites>

更具体地说,我尝试了以下

Report.WriteLine("TestSuiteName--FunctionName: 'n");
                    XDocument docs = XDocument.Load(ConfigLocation);
                    var rows = docs.Descendants("testcase").Select(x => string.Format(@"{0}-{1}--->ExecutionTime: {2}Seconds", x.Attribute("classname").Value, x.Attribute("name").Value, x.Attribute("time").Value));
                    foreach (string row in rows)
                    {
                        Report.WriteLine(row);
                    }
                    Report.WriteLine(Environment.NewLine);
                    Report.WriteLine("Failure cases: ");
                    XmlNodeList nodes1 = doc.GetElementsByTagName("testcase");
                    foreach (XmlNode node in nodes1)
                    {
                        if( node.HasChildNodes)
                        Report.WriteLine(node.FirstChild.Attributes[0].Value);
                    }

所以基本上我的疑问是,在这里我能够得到结果在单独的块,我能以某种方式合并它。

在c#中解析Report.xml

我将使用xsd工具直接从这个模式或xml文件生成类