0

这是使用 C# 序列化后我的 XML 的样子。请让我知道如何为此定义 C# 类。

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

提前致谢。

4

1 回答 1

1

通过运行您的 XMLXSD.EXE会产生以下架构:

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

再次运行该模式会XSD.EXE产生以下类:

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

可以简化为:

[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; }
}
于 2013-08-07T19:46:03.127 回答