开发者

How would you write a serializable class for this xml file?

I have this as a XML file and I'd like to write a serializable class to use with XmlSerializer.Deserialize.

Here's the file:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot 
    xmlns:od="urn:schemas-micros开发者_如何学编程oft-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>

I did something like this for a single 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; }
}

And it works fine. Now the only thing is to put AFFEANALLECT inside some kind of hash array named "dataroot".

Could you point me in the right direction please?


Create a class like AFFEANALLECTCOLLECTION:

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

}


Aliostad has posted a perfect answer for the specific case; but in the general case (of having an xml fragement):

  • save the sample somewhere as foo.xml
  • at the vs command prompt, run xsd foo.xml (this generates an xsd from the xml)
  • at the vs command prompt, run xsd foo.xsd /classes (this generates C# from the xsd)
  • copy from foo.cs

In this case (using all the default options etc):

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


Have a look at the attribute XMLSerializable and its options, you can have fine control of the resulting namespace and properties of the xml document.


If I am not wrong if you use List<AFFEANALLECT> and serialise this list, you should be able to get the format you seek

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜