【发布时间】:2015-05-23 06:53:35
【问题描述】:
我一直在尝试将一些 XML 反序列化为一个类,该类是另一个类的子类。当我尝试反序列化为基类时,它可以工作。但是,当该类被反序列化为任何子类时,它将失败。
我想知道为什么会这样。这是违反 OOP 设计还是我只是错过了一些东西。谢谢
下面是代码:
基类:Shape.cs
namespace Shape
{
using System.Xml.Serialization;
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(IsNullable = false)]
[KnownType(typeof(Rectangle))]
[KnownType(typeof(Square))]
public partial class Shape
{
private string widthField;
private string heightField;
private string colorField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Width
{
get
{
return this.widthField;
}
set
{
this.widthField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Height
{
get
{
return this.heightField;
}
set
{
this.heightField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Color
{
get
{
return this.colorField;
}
set
{
this.colorField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[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 NewDataSet
{
private Shape[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Shape")]
public Shape[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
}
子类 #1:Rectangle.cs
namespace Shape
{
public class Rectangle : Shape
{
}
}
子类 #2:Square.cs
namespace Shape
{
public class Square : Shape
{
}
}
试图将 XML 反序列化为 Rectangle 的类(子类 #1)
string xmlSample = "<?xml version=\"1.0\" ?><Shape xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"><Width>2</Width><Height>5</Height><Color>Red</Color></Shape>";
//Shape.Shape shape = Utilities.ByteArrayToObject<Shape.Shape>(Utilities.XmlStringToBytes(xmlSample)); <-- Works OK
Shape.Rectangle rect = Utilities.ByteArrayToObject<Shape.Rectangle>(Utilities.XmlStringToBytes(xmlSample)); //<-- Will fail
形状的 XSD 架构:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Shape">
<xs:complexType>
<xs:sequence>
<xs:element name="Width" type="xs:string" minOccurs="0" />
<xs:element name="Height" type="xs:string" minOccurs="0" />
<xs:element name="Color" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="Shape" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
【问题讨论】:
标签: c# xml xsd deserialization xmlserializer