C#反序列化XML成多个数组只反序列化第一个数组
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 生成一个 class,它给我:
[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 文件,类型是 class "TestConfiguration" 的名称)。它只反序列化第一个产品。你能告诉我我在这里错过了什么吗?我正在寻找的是我将有一个 "Product" 数组,其中包含自己的产品代码和一组测试。
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>
标记才能反序列化。
我创建了一个 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 生成一个 class,它给我:
[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 文件,类型是 class "TestConfiguration" 的名称)。它只反序列化第一个产品。你能告诉我我在这里错过了什么吗?我正在寻找的是我将有一个 "Product" 数组,其中包含自己的产品代码和一组测试。
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>
标记才能反序列化。