2011-02-08 5 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; } 
} 

、それが正常に動作します。今、唯一のことは、 "dataroot"という名前のハッシュ配列の中にAFFEANALLECTを置くことです。

私は正しい方向に私を指摘できますか?

+1

XMLシリアル化には[Serializable]を使用しないでください。それは無視されます。 –

+0

情報ありがとう! :) – TomShreds

+1

すべての型に 'string'を使用するのではなく、正しい型(' NUMEEMPL'に 'int'など)を使用してください。 – Oded

答えて

6

AFFEANALLECTCOLLECTIONのようなクラスを作成します。

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

} 
+0

それは動作します!それは本当に素晴らしいです!どうもありがとう! – TomShreds

2

は属性XMLSerializableとそのオプションを見てください、あなたは、XML文書の結果の名前空間とプロパティの細かな制御を持つことができます。

1

私は間違っていないよ場合は、List<AFFEANALLECT>を使用して、このリストをシリアライズ場合、あなたはAliostadが特定場合のために完璧な答えを掲載している

5

を求める形式を得ることができる必要があります。しかし(XMLのfragementを有する)一般的な場合に:

  • xsd foo.xml(これはXMLからXSDを生成する)
  • を実行し、VSコマンドプロンプトでどこ
  • としてサンプルを保存コマンドプロンプト対、(すべてのデフォルトオプションなどを使用して)この場合foo.cs

からxsd foo.xsd /classes(これはXSDからC#のを生成)

  • コピーを実行します。

    //------------------------------------------------------------------------------ 
    // <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; 
         } 
        } 
    } 
    
  • 関連する問題