2011-02-08 23 views
3

我有這個XML文件,我想編寫一個可串行化的類用於XmlSerializer.Deserialize。你將如何爲這個xml文件編寫一個可序列化的類?

這裏的文件:

<?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」的哈希數組中。

請問您能指點我正確的方向嗎?

+1

不要對XML序列化使用`[Serializable]`。它被忽略。 – 2011-02-08 13:49:13

+0

感謝您的信息! :) – TomShreds 2011-02-08 13:49:33

回答

6

創建一類像AFFEANALLECTCOLLECTION

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

} 
2

看一看屬性XMLSerializable及其選項,你可以得到的命名空間和XML文檔的屬性的精細控制。

1

如果我沒看錯的,如果你使用List<AFFEANALLECT>和連載這份名單中,你應該能夠讓你尋求

格式
5

Aliostad已經發布了具體情況下,一個完美的答案;但在一般情況下(具有一個xml fragement的):

  • 保存樣品某處爲
  • 在VS命令提示,運行xsd foo.xml(這從XML生成一個xsd)
  • 在VS命令在運行xsd foo.xsd /classes(這產生C#從XSD)從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; 
         } 
        } 
    } 
    
  • 相關問題