0

Here is what my XML should look like after serialization using C# . Please let me know how to define the C# classes for this.

<select disablesorting=\"false\">"
    <option attrib1="aa" attrib2="xx">fgdgf</option>
    <option attrib1="aa" attrib2="yy">fgdgf</option>
</select>

Thanks in advance.

1 Answer 1

1

Running your XML through XSD.EXE yields the following schema:

<?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="select">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="option" nillable="true" minOccurs="0" maxOccurs="unbounded">
          <xs:complexType>
            <xs:simpleContent msdata:ColumnName="option_Text" msdata:Ordinal="2">
              <xs:extension base="xs:string">
                <xs:attribute name="attrib1" type="xs:string" />
                <xs:attribute name="attrib2" type="xs:string" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute name="disablesorting" type="xs:string" />
    </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="select" />
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

Running that schema through XSD.EXE again yields the following class:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18052
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1.
// 


/// <remarks/>
[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 select {

    private selectOption[] optionField;

    private string disablesortingField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("option", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    public selectOption[] option {
        get {
            return this.optionField;
        }
        set {
            this.optionField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string disablesorting {
        get {
            return this.disablesortingField;
        }
        set {
            this.disablesortingField = 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)]
public partial class selectOption {

    private string attrib1Field;

    private string attrib2Field;

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string attrib1 {
        get {
            return this.attrib1Field;
        }
        set {
            this.attrib1Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string attrib2 {
        get {
            return this.attrib2Field;
        }
        set {
            this.attrib2Field = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = 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 select[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("select")]
    public select[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

Which can be simplified to:

[Serializable]
[XmlType(AnonymousType=true)]
[XmlRoot(Namespace="", IsNullable=false)]
public partial class select {

    [XmlElement("option")]
    public selectOption[] option { get; set; }

    /// <remarks/>
    [XmlAttribute]
    public string disablesorting { get; set; }
}

[System.Serializable]
[XmlType(AnonymousType=true)]
public partial class selectOption {

    [XmlAttribute]
    public string attrib1 { get; set; }

    [XmlAttribute]
    public string attrib2 { get; set; }

    [XmlText]
    public string Value { get; set; }
}

[System.Serializable]
[XmlType(AnonymousType=true)]
[XmlRoot(Namespace="", IsNullable=false)]
public partial class NewDataSet {

    [XmlElement("select")]
    public select[] Items { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.