这是我的 XSD 文件:
<?xml version="1.0" encoding="UTF-8"?>
<xs:complexType name="**c_extension**">
<xs:choice maxOccurs="unbounded">
<xs:element ref="**extensionType1**"/>
<xs:element ref="**extensionType2**"/>
<xs:any namespace="##other" processContents="lax"/>
</xs:choice>
<xs:attribute name="extVersion" type="xs:decimal" use="optional" default="1.0"/>
</xs:complexType>
<xs:element name="**extensionType1**">
<xs:complexType>
<xs:complexContent>
<xs:extension base="c_extensionType1">
<xs:anyAttribute namespace="##other" processContents="lax"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:element name="**extensionType2**">
<xs:complexType>
<xs:complexContent>
<xs:extension base="c_extensionType2">
<xs:anyAttribute namespace="##other" processContents="lax"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:complexType name="c_extensionType1">
<xs:attribute name="id" type="xs:int" use="required"/>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="c_extensionType2">
<xs:attribute name="id" type="xs:int" use="required"/>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
<!--root element-->
<xs:element name="extension">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:complexContent>
<xs:extension base="c_extension">
<xs:anyAttribute namespace="##other" processContents="lax"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:schema>
这是从上面的 XSD 文件生成的代码:
namespace My.Extension {
using System.Xml.Serialization;
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://myextension.com/ProgInfo")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://myextension.com/ProgInfo", IsNullable=false)]
public partial class extensionType1 : c_extensionType1{
private System.Xml.XmlAttribute[] anyAttrField;
[System.Xml.Serialization.XmlAnyAttributeAttribute()]
public System.Xml.XmlAttribute[] AnyAttr { get { return this.anyAttrField; } set {this.anyAttrField = value; } }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://myextension.com/ProgInfo")]
public partial class c_extensionType1 {
private id idField;
private string nameField;
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
public string id {get {return this.idField;}set {this.idField = value;}}
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
public string name {get {return this.nameField;}set {this.nameField = value;}}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://myextension.com/ProgInfo")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://myextension.com/ProgInfo", IsNullable=false)]
public partial class extensionType2 : c_extensionType2{
private System.Xml.XmlAttribute[] anyAttrField;
[System.Xml.Serialization.XmlAnyAttributeAttribute()]
public System.Xml.XmlAttribute[] AnyAttr { get { return this.anyAttrField; } set {this.anyAttrField = value; } }
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://myextension.com/ProgInfo")]
public partial class c_extensionType2 {
private id idField;
private string nameField;
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
public string id {get {return this.idField;}set {this.idField = value;}}
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
public string name {get {return this.nameField;}set {this.nameField = 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, Namespace="http://myextension.com/ProgInfo")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://myextension.com/ProgInfo", IsNullable=false)]
public partial class extension : c_extension {
private System.Xml.XmlAttribute[] anyAttrField;
[System.Xml.Serialization.XmlAnyAttributeAttribute()]
public System.Xml.XmlAttribute[] AnyAttr {get {return this.anyAttrField;}set {this.anyAttrField = value;}}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://myextension.com/ProgInfo")]
public partial class c_extension {
**private object[] itemsField**;
private decimal extVersionField;
public c_igtVendorExt() {this.extVersionField = ((decimal)(1.0m));}
/// <remarks/>
[System.Xml.Serialization.XmlAnyElementAttribute(Order=0)]
[System.Xml.Serialization.XmlElementAttribute("extensionType1", typeof(extensionType1), Order=0)]
[System.Xml.Serialization.XmlElementAttribute("extensionType2", typeof(extensionType2), Order=0)]
public object[] Items{get {return this.itemsField;}set {this.itemsField = value;}}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
[System.ComponentModel.DefaultValueAttribute(typeof(decimal), "1.0")]
public decimal extVersion {get {return this.extVersionField;}set {this.extVersionField = value;}}
}
}
这是我的真实数据,在 extension.xml 文件中:
<?xml version="1.0" encoding="UTF-8"?>
<extension extVersion="1.03">
<extensionType1 id="1" name="GAME"/>
<extensionType1 id="2" name="GAME"/>
</extension>
这是我对扩展进行反序列化的方法:
string extensionXml = System.IO.File.ReadAllText("extension.xml");
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "extension";
xRoot.IsNullable = true;
XmlSerializer xs = new XmlSerializer(typeof(extension), xRoot);
TextReader tr = new StringReader(extensionXml);
extension ext = (extension)xs.Deserialize(tr);
ext.Items 确实有 2 个项目,但是当我检查 ext.Items 中的项目类型时,它是 XmlElement。为什么不是 extensionType1?
有人能告诉我为什么吗?是不是因为扩展中的子节点有两个类型节点?扩展类型 1 和扩展类型 2。