如何使用C#定义ArrayItemAttribute和ArrayItem.in XML序列化中存在的属性

时间:2013-08-07 19:39:17

标签: c# xml xml-serialization

以下是使用C#进行序列化后我的XML应该是什么样子。请告诉我如何为此定义C#类。

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

提前致谢。

1 个答案:

答案 0 :(得分:1)

通过XSD.EXE运行XML会产生以下架构:

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