3

ここに私の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 ではないのですか?

誰かが理由を教えてもらえますか? 拡張中の子ノードに型ノードが 2 つあるためでしょうか。拡張タイプ 1 および拡張タイプ 2。

4

0 に答える 0