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

C# 如何为这个xml文件编写一个可序列化的类?,c#,.net,xml-serialization,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:

我将其作为一个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>
而且效果很好。现在唯一的事情就是将Affenallect放在一个名为“dataroot”的散列数组中


你能给我指一下正确的方向吗?

创建一个类似于
AffanallectCollection
的类:

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

}
[XmlRoot(“dataroot)]
公共类集合
{
[XmlElement(“AffenalSelect”)]
公共列表参数{get;set;}
}

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

如果我没有错,如果您使用
列表并序列化此列表,您应该能够获得所需的格式Aliostad已经发布了针对特定情况的完美答案;但在一般情况下(具有xml碎片):

  • 将示例另存为
    foo.xml
  • 在vs命令提示符下,运行
    xsd foo.xml
    (这将从xml生成xsd)
  • 在vs命令提示符下,运行
    xsd foo.xsd/classes
    (这将从xsd生成C)
  • foo.cs
在这种情况下(使用所有默认选项等):

//------------------------------------------------------------------------------
// 
//这段代码是由一个工具生成的。
//运行时版本:4.0.30319.1
//
//对此文件的更改可能会导致不正确的行为,如果
//重新生成代码。
// 
//------------------------------------------------------------------------------
使用System.Xml.Serialization;
// 
//此源代码由xsd自动生成,版本=4.0.30319.1。
// 
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分类dataroot{
私有DataRootAffenallect[]Affenallect字段;
私有字符串生成字段;
/// 
[System.Xml.Serialization.xmlementAttribute(“Affenallect”,Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public datarootaffenallect[]affenallect{
得到{
返回此.affenallect字段;
}
设置{
this.affenallectfield=值;
}
}
/// 
[System.Xml.Serialization.XmlAttributeAttribute()]
生成的公共字符串{
得到{
返回此.generatedField;
}
设置{
this.generatedField=值;
}
}
}
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
公共部分类DataRootAffenallect{
私有字符串numemplfield;
私有字符串tYPETRAVField;
私有字符串字段;
私有字符串日期字段;
私有字符串sTATIONField;
私有字符串字段;
私有字符串源字段;
/// 
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串numempl{
得到{
返回this.numemplfield;
}
设置{
this.numemplfield=值;
}
}
/// 
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串TYPETRAV{
得到{
返回此.tYPETRAVField;
}
设置{
this.tYPETRAVField=值;
}
}
/// 
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串合同{
得到{
返回此.contatfield;
}
设置{
this.cONTRATField=值;
}
}
/// 
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串日期{
得到{
返回此.dATEField;
}
设置{
this.dATEField=值;
}
}
/// 
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共弦乐台{
得到{
返回此.sTATIONField;
}
设置{
this.sTATIONField=值;
}
}
/// 
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串HORAIRE{
得到{
返回此.hORAIREField;
}
设置{
this.hORAIREField=值;
}
}
/// 
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共字符串源{
得到{
返回此.oRIGINEField;
}
设置{
this.oRIGINEField=值;
}
}
}
/// 
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”,“4.0.30319.1”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace=”“,IsNullable=false)]
公共部分类NewDataSet{
私有dataroot[]itemsField;
/// 
[System.Xml.Serialization.xmlementAttribute(“dataroot”)]
公共数据根[]项{
得到{
返回此.itemsField;
}
设置{
this.itemsField=值;
}
}
}

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

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