3

Here my XSD file:

            <?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>

Here is code generate from XSD file in above:

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;}}
                    }
                }

Here is my real data, in extension.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<extension extVersion="1.03">
  <extensionType1 id="1" name="GAME"/>
  <extensionType1 id="2" name="GAME"/>
</extension>

Here is how i do deserialize for 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 has 2 items indeed, but when i check type of item in ext.Items, it's XmlElement. Why not extensionType1?

Can someone tell me why? Is it because the child node in extension has two type nodes? extensionType1 and extensionType2.

Rick
  • 95
  • 5
  • I figure out finally, it's due to the namespace in my xsd file; so there must be namespace specificed in my data file extension.xml. – Rick Sep 26 '12 at 17:53

0 Answers0