C#将XML反序列化为多个数组只反序列化第一个数组

时间:2015-06-26 08:31:32

标签: c# arrays xml xsd

我创建了一个XSD,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="TestConfiguration">
        <xs:complexType>
        <xs:sequence>

        <xs:element name="Products" minOccurs="1" maxOccurs="1">
        <xs:complexType><xs:sequence>
        <xs:element name="ProductCode" type="xs:string"  minOccurs="1" maxOccurs="1"></xs:element>
        <xs:element name="Tests" type="TestName" minOccurs="1" maxOccurs="1"></xs:element>
        </xs:sequence></xs:complexType>
        </xs:element>

        </xs:sequence>
        </xs:complexType>
    </xs:element>

<xs:complexType name="TestName">
<xs:sequence>
<xs:element name="Test" minOccurs="1" maxOccurs="unbounded" type="xs:string"></xs:element>
</xs:sequence>

</xs:complexType>
</xs:schema>

XSD用于生成以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<TestConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="TibTestConfiguration.xsd">
    <Product>
        <ProductCode>B3-00</ProductCode>
        <Tests>
            <Test>DB9 Serial Port Connector</Test>
            <Test>GPRS MODEM</Test>
            <Test>SDRAM</Test>              
    </Product>
    <Product>
        <ProductCode>B3-01</ProductCode>
        <Tests>
            <Test>DB9 Serial Port Connector</Test>
            <Test>GPRS MODEM</Test>
            <Test>NAND Flash</Test>
            <Test>Main DC Power Port</Test>         
    </Product>
</TestConfiguration>

然后,我使用Microsoft Visual Studio xsd.exe从XSD生成一个类,它给了我:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]

    public partial class TestConfiguration
    {

        private TestConfigurationProducts productsField;


        public TestConfigurationProducts Product
        {
            get
            {
                return this.productsField;
            }
            set
            {
                this.productsField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class TestConfigurationProducts
    {

        private string productCodeField;

        private string[] testsField;

        /// <remarks/>
        public string ProductCode
        {
            get
            {
                return this.productCodeField;
            }
            set
            {
                this.productCodeField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("Test", IsNullable = false)]
        public string[] Tests
        {
            get
            {
                return this.testsField;
            }
            set
            {
                this.testsField = value;
            }
        }
    }

当我使用以下代码对其进行反序列化时(文件是xml文件,类型是类的名称&#34; TestConfiguration&#34;)。它只反序列化第一个产品。你能告诉我这里错过了什么吗?我正在寻找的是我将拥有一系列产品&#34;产品&#34;它包含自己的产品代码和一系列测试。

public static object DeserialiseXML(string file, Type type)
{
    string errorMsg = string.Empty;

    try
    {
        XmlSerializer serializer = new XmlSerializer(type);

        using (StreamReader reader = new StreamReader(file))
        {
            //Deserialize to object
            var deserialisedObject = serializer.Deserialize(reader);
            reader.Close();

            return deserialisedObject;
        }
    }

    catch (Exception ex)
    {
        errorMsg = ex.Message;
        return null;
    }
}       

1 个答案:

答案 0 :(得分:1)

根据您的架构,您的XML无效。架构表示您的Product元素称为Products,并且只能出现一次(maxOccurs="1"),这就是为什么结果类只有一个Product而不是一个数组它们。

虽然你可以解决这个问题并重新生成,但下面的类应该有效:

public class TestConfiguration
{
    [XmlElement("Product")]
    public Product[] Products { get; set; }
}

public class Product
{
    public string ProductCode { get; set; }

    [XmlArrayItem("Test")]
    public string[] Tests { get; set; }
}

另外,您的XML格式不正确 - 您需要在反序列化之前添加结束</Tests>标记。