[英]C# Deserialise XML into mutiple arrays only deserialise the first array
我创建了一个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文件,类型是类“ TestConfiguration”的名称)。 它只会反序列化第一个产品。 您能告诉我我在这里错过了什么吗? 我要寻找的是一个“产品”数组,其中包含自己的产品代码和测试数组。
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;
}
}
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>
标记,然后才能进行反序列化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.