我想解析树,但似乎许多节点也具有相同的名称和属性,因此尝试以有效的方式解析它,以便可以通过类重用它。
<TestStructure>
<TestStructureElement Id="ID_TSE_6" Type="FunctionHierarchy" ObjectRef="ID_FuncHierarchy_7" T42ElementId="31391123">
<TestStructureElement Id="ID_TSE_12" Type="Function" ObjectRef="ID_Function_13" T42ElementId="31391126">
<TestStructureElement Id="ID_TSE_14" Type="Function" ObjectRef="ID_Function_15" T42ElementId="31391127">
<TestStructureElement Id="ID_TSE_16" Type="Function" ObjectRef="ID_Function_17" T42ElementId="31391128"/>
</TestStructureElement>
<TestStructureElement Id="ID_TSE_18" Type="Function" ObjectRef="ID_Function_19" T42ElementId="31391129">
<TestStructureElement Id="ID_TSE_20" Type="Function" ObjectRef="ID_Function_21" T42ElementId="31391130"/>
</TestStructureElement>
<TestStructureElement Id="ID_TSE_26" Type="TestCase" ObjectRef="ID_TestCase_27" T42ElementId="31391133" DOORSId="ESP-TC-104">
<TestResults TestState="EXECUTED">
<Evaluations>
<Evaluation VDSEvaluation="VDS_8">
<Remark>Hochreibwert =1 nicht gegeben</Remark>
</Evaluation>
</Evaluations>
<TestCaseActivities>
<TestCaseActivity TCActivity="TESTCASE" TCActivityState="NONE"/>
<TestCaseActivity TCActivity="VDS" TCActivityState="NONE"/>
<TestCaseActivity TCActivity="SETUP" TCActivityState="NONE"/>
<TestCaseActivity TCActivity="RESUBMISSION" TCActivityState="NONE"/>
</TestCaseActivities>
</TestResults>
</TestStructureElement>
</TestStructure>
答案 0 :(得分:1)
试试xml linq。我使用递归算法 下面的代码只假设一个测试报告并使其成为静态
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
new TestStructureElement(FILENAME);
}
}
public class TestStructureElement
{
public static TestStructureElement root = new TestStructureElement();
public string id { get; set; }
public string _Type { get; set;}
public string objectRef { get; set; }
public int t42ElementId { get;set;}
public List<TestStructureElement> children { get;set;}
public static TestResults testResults { get; set; }
public TestStructureElement() { }
public TestStructureElement(string filename)
{
XDocument doc = XDocument.Load(filename);
XElement testStructure = doc.Descendants("TestStructureElement").FirstOrDefault();
GetTree(testStructure, root);
}
public void GetTree(XElement xml, TestStructureElement testStructure)
{
testStructure.id = (string)xml.Attribute("Id");
testStructure._Type = (string)xml.Attribute("Type");
testStructure.objectRef = (string)xml.Attribute("ObjectRef");
testStructure.t42ElementId = (int)xml.Attribute("T42ElementId");
if (xml.Elements("TestStructureElement") != null)
{
testStructure.children = new List<TestStructureElement>();
foreach (XElement child in xml.Elements("TestStructureElement"))
{
TestStructureElement childStructure = new TestStructureElement();
testStructure.children.Add(childStructure);
GetTree(child, childStructure);
}
}
TestStructureElement.testResults = xml.Descendants("TestResults").Select(x => new TestResults()
{
testState = (string)x.Attribute("TestState"),
evalaution = (string)x.Descendants("Evaluation").FirstOrDefault().Attribute("VDSEvaluation"),
evaluationRemark = (string)x.Descendants("Evaluation").FirstOrDefault().Element("Remark"),
testCaseActivity = x.Descendants("TestCaseActivity")
.GroupBy(y => (string)y.Attribute("TCActivity"), z => (string)z.Attribute("TCActivityState"))
.ToDictionary(y => y.Key, z => z.FirstOrDefault())
}).FirstOrDefault();
}
}
public class TestResults
{
public string testState { get; set; }
public string evalaution { get; set; }
public string evaluationRemark { get; set; }
public Dictionary<string, string> testCaseActivity { get; set; }
}
}
以下代码有一项更改,但不会使测试报告静态
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
new TestStructureElement(FILENAME);
}
}
public class TestStructureElement
{
public static TestStructureElement root = new TestStructureElement();
public string id { get; set; }
public string _Type { get; set;}
public string objectRef { get; set; }
public int t42ElementId { get;set;}
public List<TestStructureElement> children { get;set;}
public TestResults testResults { get; set; }
public TestStructureElement() { }
public TestStructureElement(string filename)
{
XDocument doc = XDocument.Load(filename);
XElement testStructure = doc.Descendants("TestStructureElement").FirstOrDefault();
GetTree(testStructure, root);
}
public void GetTree(XElement xml, TestStructureElement testStructure)
{
testStructure.id = (string)xml.Attribute("Id");
testStructure._Type = (string)xml.Attribute("Type");
testStructure.objectRef = (string)xml.Attribute("ObjectRef");
testStructure.t42ElementId = (int)xml.Attribute("T42ElementId");
if (xml.Elements("TestStructureElement") != null)
{
testStructure.children = new List<TestStructureElement>();
foreach (XElement child in xml.Elements("TestStructureElement"))
{
TestStructureElement childStructure = new TestStructureElement();
testStructure.children.Add(childStructure);
GetTree(child, childStructure);
}
}
testStructure.testResults = xml.Descendants("TestResults").Select(x => new TestResults()
{
testState = (string)x.Attribute("TestState"),
evalaution = (string)x.Descendants("Evaluation").FirstOrDefault().Attribute("VDSEvaluation"),
evaluationRemark = (string)x.Descendants("Evaluation").FirstOrDefault().Element("Remark"),
testCaseActivity = x.Descendants("TestCaseActivity")
.GroupBy(y => (string)y.Attribute("TCActivity"), z => (string)z.Attribute("TCActivityState"))
.ToDictionary(y => y.Key, z => z.FirstOrDefault())
}).FirstOrDefault();
}
}
public class TestResults
{
public string testState { get; set; }
public string evalaution { get; set; }
public string evaluationRemark { get; set; }
public Dictionary<string, string> testCaseActivity { get; set; }
}
}