首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解析此xml树

如何解析此xml树
EN

Stack Overflow用户
提问于 2017-05-20 16:47:36
回答 1查看 611关注 0票数 0

我想解析树,但是看起来很多节点的名称和属性都是相同的,所以尝试以一种高效的方式解析它,这样就可以通过类重用它。

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-20 17:52:33

试试。下面的递归算法代码只假设一个测试报告,并使其成为静态的

代码语言:javascript
复制
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; }
    }

}

下面的代码有一个不使测试报告静态的更改

代码语言:javascript
复制
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; }
    }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44088483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档