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

Tom*_* B. 3 .net c# 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>
Run Code Online (Sandbox Code Playgroud)

我为一个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; }
}
Run Code Online (Sandbox Code Playgroud)

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

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

Ali*_*tad 6

创建一个类,如AFFEANALLECTCOLLECTION:

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

}
Run Code Online (Sandbox Code Playgroud)


Mar*_*ell 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)