【问题标题】:XML deserialization failed to load values to element containing array of elementsXML 反序列化无法将值加载到包含元素数组的元素
【发布时间】:2015-06-08 13:33:05
【问题描述】:

我有以下 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<TestConfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Barcode>MB-B3-00</Barcode>
    <TestSuites>
        <Test>USB A Slave Port</Test>
        <Test>USB B Host Port</Test>
    </TestSuites>
</TestConfiguration>

我想把它反序列化成下面的类:

 public class TestConfiguration
    {
        private string _barcode;
        private string[] _testSuites;
        private string[] _testcase;

        //Product barcode
        public string Barcode
        {
            get{return this._barcode;}
            set{this._barcode = value;}
        }       

        //Test suites
        [System.Xml.Serialization.XmlArrayItemAttribute("Test", IsNullable = false)]       
        public string[] Testsuites
        {
            get{return this._testSuites;}
            set{this._testSuites = value;}
        }

        //individual test
        [System.Xml.Serialization.XmlTextAttribute()]
        public string[] Testcase
        {
            get{return this._testcase;}
            set{this._testcase = value;}
        }
    }

我的反序列化代码是:

XmlSerializer serializer = new XmlSerializer(typeof(TestConfiguration));
StreamReader reader = new StreamReader(filename);
TestConfiguration _testConfig = (TestConfiguration)serializer.Deserialize(reader);
reader.Close();

但是,_testConfig 对象仅包含 Barcode 值,属性 TestcaseTestSuites 为空。请问有什么建议吗?

【问题讨论】:

  • 您错误地使用了 XmlArrayAttribute - 请参阅此问题:stackoverflow.com/questions/15907357/…。你需要定义另一个类来做你想做的事情。
  • 如果手动填充类,然后将其序列化为 XML,XML 会是什么样子?
  • 您希望在Testcase 中看到什么值?

标签: c# xml


【解决方案1】:

你很亲密。您的属性名称Testsuites 与元素名称&lt;TestSuites&gt;完全匹配 - 字母S 的大小写不同,XML Tags are Case Sensitive

要修复,重命名属性或使用正确的element name 附加XmlArrayAttribute

    //Test suites
    [System.Xml.Serialization.XmlArray("TestSuites")]
    [System.Xml.Serialization.XmlArrayItem("Test", IsNullable = false)]
    public string[] Testsuites
    {
        get { return this._testSuites; }
        set { this._testSuites = value; }
    }

【讨论】:

    【解决方案2】:

    试试这个。您可以消除标签并拥有一个仅包含元素的数组。我可以告诉你怎么做。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(TestConfiguration));
                StreamReader reader = new StreamReader(FILENAME);
                TestConfiguration _testConfig = (TestConfiguration)serializer.Deserialize(reader);
                reader.Close();
    
            }
        }
    
        [XmlRoot("TestConfiguration")]
        public class TestConfiguration
        {
            private string _barcode;
            private string[] _testSuites;
            private string[] _testcase;
    
            //Product barcode
            [XmlElement("Barcode")]
            public string Barcode { get; set; }
    
            //Test suites
            [XmlElement("TestSuites")]
            public TestSuites testSuites { get; set; }
    
        }
        //individual test
        [XmlRoot("TestSuites")]
        public class TestSuites
        {
            [XmlElement("Test")]
            public List<string> test {get;set;}
        }
    
    }
    ​
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-25
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多