你会如何为这个xml文件编写一个可序列化的类?

时间:2011-02-08 13:47:16

标签: c# .net xml-serialization

我将它作为XML文件,我想编写一个可序列化的类来与XmlSerializer.Deserialize一起使用。

这是文件:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot 
    xmlns:od="urn:schemas-microsoft-com:officedata"     
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
    xsi:noNamespaceSchemaLocation="AFFEANALLECT.xsd" 
    generated="2011-02-02T13:27:46">
    <AFFEANALLECT>
        <NUMEEMPL>4</NUMEEMPL>
        <TYPETRAV>SOUD</TYPETRAV>
        <CONTRAT>08245</CONTRAT>
        <DATE>2008-03-27 14:09:59</DATE>
        <STATION>02</STATION>
        <HORAIRE>1</HORAIRE>
        <ORIGINE>AFFE FIN</ORIGINE>
    </AFFEANALLECT>
    <AFFEANALLECT>
        <NUMEEMPL>4</NUMEEMPL>
        <TYPETRAV>SOUD</TYPETRAV>
        <CONTRAT>08245</CONTRAT>
        <DATE>2008-03-27 08:29:46</DATE>
        <STATION>02</STATION>
        <HORAIRE>1</HORAIRE>
        <ORIGINE>AFFE DEBUT</ORIGINE>
    </AFFEANALLECT>
</dataroot>

我为一个AFFEANALLECT做了类似的事情:

[Serializable()]
public class AFFEANALLECT
{
    public string NUMEEMPL { get; set; }
    public string TYPETRAV { get; set; }
    public string CONTRAT  { get; set; }
    public string DATE     { get; set; }
    public string STATION  { get; set; }
    public string HORAIRE  { get; set; }
    public string ORIGINE  { get; set; }
}

它工作正常。现在唯一的事情就是将AFFEANALLECT放在某种名为“dataroot”的哈希数组中。

你能指出我正确的方向吗?

4 个答案:

答案 0 :(得分:6)

创建一个类AFFEANALLECTCOLLECTION

[XmlRoot("dataroot)]
public class AFFEANALLECTCOLLECTION
{
    [XmlElement("AFFEANALLECT")]
    public List<AFFEANALLECT> AFFEANALLECTS {get; set;}

}

答案 1 :(得分:5)

Aliostad已经为特定的案例发布了一个完美答案;但在一般情况下(具有xml fragement):

  • 将样本保存为foo.xml
  • 在vs命令提示符下,运行xsd foo.xml(这会从xml生成xsd)
  • 在vs命令提示符下,运行xsd foo.xsd /classes(这会从xsd生成C#)
  • foo.cs
  • 复制

在这种情况下(使用所有默认选项等):

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.1
//
//     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.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 dataroot {

    private datarootAFFEANALLECT[] aFFEANALLECTField;

    private string generatedField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("AFFEANALLECT", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public datarootAFFEANALLECT[] AFFEANALLECT {
        get {
            return this.aFFEANALLECTField;
        }
        set {
            this.aFFEANALLECTField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string generated {
        get {
            return this.generatedField;
        }
        set {
            this.generatedField = 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 datarootAFFEANALLECT {

    private string nUMEEMPLField;

    private string tYPETRAVField;

    private string cONTRATField;

    private string dATEField;

    private string sTATIONField;

    private string hORAIREField;

    private string oRIGINEField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string NUMEEMPL {
        get {
            return this.nUMEEMPLField;
        }
        set {
            this.nUMEEMPLField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string TYPETRAV {
        get {
            return this.tYPETRAVField;
        }
        set {
            this.tYPETRAVField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string CONTRAT {
        get {
            return this.cONTRATField;
        }
        set {
            this.cONTRATField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string DATE {
        get {
            return this.dATEField;
        }
        set {
            this.dATEField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string STATION {
        get {
            return this.sTATIONField;
        }
        set {
            this.sTATIONField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string HORAIRE {
        get {
            return this.hORAIREField;
        }
        set {
            this.hORAIREField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string ORIGINE {
        get {
            return this.oRIGINEField;
        }
        set {
            this.oRIGINEField = 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 dataroot[] itemsField;

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

答案 2 :(得分:2)

查看属性XMLSerializable及其选项,您可以很好地控制生成的命名空间和xml文档的属性。

答案 3 :(得分:1)

如果您使用List<AFFEANALLECT>并序列化此列表我没有错,您应该能够获得您寻找的格式

相关问题