简体   繁体   English

如何解析这个xml树

[英]How to parse this xml tree

I want to parse tree but it seems many nodes have the same name and attribute as well, so trying to parse it in a efficient way so it can be reused via classes. 我想解析树,但似乎许多节点也具有相同的名称和属性,因此尝试以一种有效的方式解析它,以便可以通过类重用它。

<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>

Try xml linq. 尝试xml linq。 I usd a recursive algorithm Code below assumes only one Test Report and makes it static 我使用一种递归算法下面的代码仅假设一个测试报告并使之静态

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; }
    }

}

Code below has one change which doesn't make Test Report Static 下面的代码有一项更改不会使测试报告成为静态

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; }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM