【发布时间】:2013-11-23 16:26:37
【问题描述】:
我用 xsd.exe 生成了以下类:
using System.Xml.Serialization;
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[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 ROOT {
private PersonBaseType pERSONField;
/// <remarks/>
public PersonBaseType PERSON {
get {
return this.pERSONField;
}
set {
this.pERSONField = value;
}
}
}
[System.Xml.Serialization.XmlIncludeAttribute(typeof(NotWorkerPersonType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(WorkerPersonType))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public abstract partial class PersonBaseType {
private MotorVehicleType vEHICLEField;
public MotorVehicleType VEHICLE {
get {
return this.vEHICLEField;
}
set {
this.vEHICLEField = value;
}
}
}
[System.Xml.Serialization.XmlIncludeAttribute(typeof(CarVehicleType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(AutobusVehicleType))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public abstract partial class MotorVehicleType {
private string nameField;
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class CarVehicleType : MotorVehicleType {
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class AutobusVehicleType : MotorVehicleType {
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class NotWorkerPersonType : PersonBaseType {
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class WorkerPersonType : PersonBaseType {
}
我已经使用工具 xsd.exe 和以下 xsd 架构生成了这个类:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:complexType name="PersonBaseType" abstract="true">
<xsd:sequence>
<xsd:element name="VEHICLE" type="MotorVehicleType" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="WorkerPersonType">
<xsd:complexContent>
<xsd:restriction base="PersonBaseType">
<xsd:sequence>
<xsd:element name="VEHICLE" type="AutobusVehicleType" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="NotWorkerPersonType">
<xsd:complexContent>
<xsd:restriction base="PersonBaseType">
<xsd:sequence>
<xsd:element name="VEHICLE" type="CarVehicleType" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="MotorVehicleType" abstract="true">
<xsd:sequence>
<xsd:element name="Name" type="xsd:string" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="AutobusVehicleType">
<xsd:complexContent>
<xsd:restriction base="MotorVehicleType">
<xsd:sequence>
<xsd:element name="Name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="CarVehicleType">
<xsd:complexContent>
<xsd:restriction base="MotorVehicleType">
<xsd:sequence>
<xsd:element name="Name" type="xsd:string" minOccurs="0" maxOccurs="1"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name="ROOT">
<xsd:annotation>
<xsd:documentation>Root element</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="PERSON" type="PersonBaseType" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
我尝试反序列化这个 xml:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT xmlns:xxx="http://www.w3.org/2001/XMLSchema-instance" xxx:schemaLocation="C:\Users\SIGFRID\Documents\Visual Studio 2012\Projects\sample\ConsoleApplication1\ConsoleApplication1\MySample.xsd">
<PERSON xxx:type="WorkerPersonType">
<VEHICLE>
<Name>VOLVO</Name>
</VEHICLE>
</PERSON>
</ROOT>
Schema Validation 是正确的,我没有问题,但随后我得到一个带有消息的InvalidOperationException:
The specified type is abstract: name='MotorVehicleType', Namespace='', bei <VEHICLE xmlns=''>."}
我知道我应该指定 VEHICLE 的类型,但为什么 xml 会通过模式验证?如果 Person 元素是 WorkerPersonType,则 VEHICLE 只能是 AutobusVehicleType。这就是我在 xsd 架构中指定人员的方式。
我应该改变我的班级吗?我应该添加一些属性还是其他东西?
我不知道如何解决这个错误!
【问题讨论】:
标签: c# xml serialization xsd