【问题标题】:Deserialize XML collection with possible different elements but same class使用可能的不同元素但相同的类反序列化 XML 集合
【发布时间】:2016-06-10 06:53:24
【问题描述】:

我正在尝试为失败的测试编写 Nunit 报告分析器/收集器之类的东西,但在尝试反序列化测试报告时卡住了。

Nunit 报告具有以下结构:

<test-results ... >
    <test-suite>
        <results>
            <test-suite> or <test-case> bunch of elements
                <failure> // optional
        </results>
    </test-suite>
</test-results>

所以 test-suite 元素可以具有包含其他测试套件元素的结果集合或包含测试用例元素的结果集合。 由于测试套件具有与测试用例相同的属性,因此可以将其序列化为一种类型的类:

[Serializable()]
public class TestResult
{
    [XmlAttribute("name")]
    public String Name { get; set; }
    [XmlAttribute("executed")]
    public String Executed { get; set; }

    [XmlAttribute("success")]
    public String Success { get; set; }

    [XmlElement("failure", IsNullable = true)]
    public Failure Failure { get; set; }

    [XmlElement("results")]
    public Results Results { get; set; }

    [XmlAttribute("result")]
    public String Result { get; set; }

    [XmlAttribute("time")]
    public String Time { get; set; }

    [XmlAttribute("asserts")]
    public String Asserts { get; set; }    
}

[Serializable()]
public class TestCase : TestResult
{

}

[Serializable()]
public class TestSuite : TestResult
{
    [XmlAttribute("type")]
    public String Type { get; set; }
}

Results 类假设有一个测试套件或测试用例列表:

[Serializable()]
    public class Results
    {
        [XmlArray("results")]
        [XmlArrayItem("test-case", Type = typeof(TestCase))]
        [XmlArrayItem("test-suite", Type = typeof(TestSuite))]
        public List<Result> Result { get; set; }
    }

这里的TestCase 和TestSuite 是Result 的空子类,因为arrtibutes 和元素是相同的。没有以这种方式从集合中序列化的项目。如果我试图为每个项目指定多个没有专用类型的 ArrauItem-s,则解析器认为这是明确的。

我怎样才能真正用不同但相关的元素来序列化集合?

【问题讨论】:

    标签: c# xml serialization


    【解决方案1】:

    我相信下面的结构会序列化为您想要的架构(删除了非必要属性):

    public class TestResult
    {
        ...
    
        [XmlElement("results")]
        public TestResultsCollection Results { get; set; }
    
        ...
    }
    
    public class TestCase : TestResult
    {
    }
    
    public class TestSuite : TestResult
    {
        [XmlAttribute("type")]
        public String Type { get; set; }
    }
    
    [XmlRoot("test-results")]
    public class TestResultsCollection
    {
        [XmlElement("test-case", Type = typeof(TestCase))]
        [XmlElement("test-suite", Type = typeof(TestSuite))]
        public List<TestResult> Results { get; set; }
    }
    

    这是我使用它来序列化/反序列化的方式:

    var serializer = new XmlSerializer(typeof (TestResultsCollection));
    
    var testResultsCollection = new TestResultsCollection();
    testResultsCollection.Items = new List<TestResult>
    {
        new TestSuite
        {
            Results = new TestResultsCollection
            {
                Items = new List<TestResult> {new TestCase(), new TestSuite()}
            }
        },
        new TestCase
        {
            Results = new TestResultsCollection
            {
                Items = new List<TestResult> {new TestCase(), new TestSuite()}
            }
        }
    };
    
    using (var fileStream = File.CreateText("output.xml"))
    {
        serializer.Serialize(fileStream, testResultsCollection);
    }
    
    using (var fileStream = File.OpenText("output.xml"))
    {
        var deserialized = (TestResultsCollection)serializer.Deserialize(fileStream);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 2017-02-07
      相关资源
      最近更新 更多