2011-09-30 57 views
0

我有很多的文件,用的格式:XMLSERIALIZE - Desrialize外部XML

<?xml version="1.0" encoding="utf-8"?> 
<Words> 
    <word> 
    <Eng>chain</Eng> 
    <EngEnd>chained</EngEnd> 
    <PartOfSpeechEng>verb</PartOfSpeechEng> 
    <Heb>לקשור עם שרשרת</Heb> 
    <EngInHeb>צֵ'ין</EngInHeb> 
    <PartOfSpeechHeb>פועל</PartOfSpeechHeb> 
    <DicNumber>27</DicNumber> 
    <Arb>سلسلة</Arb> 
    <EngInArb> 
    </EngInArb> 
    </word> 
    <word> 
    <Eng>growl</Eng> 
    <EngEnd>growls</EngEnd> 
    <PartOfSpeechEng>verb</PartOfSpeechEng> 
    <Heb>לנהום</Heb> 
    <EngInHeb>גְרַאוּל</EngInHeb> 
    <PartOfSpeechHeb>פועל</PartOfSpeechHeb> 
    <DicNumber>3</DicNumber> 
    <Arb>دمدمة/تذمـُّر</Arb> 
    <EngInArb> 
    </EngInArb> 
    </word> 
</Words> 

我沒把它反序列化到陣列中,有人可以幫我用反序列化

感謝

+0

是什麼類是什麼樣子? –

+0

我只想把它列入數組 – gilhanan

+0

請顯示您嘗試的代碼以及它造成的問題。 –

回答

1

生成xsd.exe使用由this online tool創建的模式。

//------------------------------------------------------------------------------ 
// <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 Words { 

    private WordsWord[] wordField; 

    /// <remarks/> 
    [System.Xml.Serialization.XmlElementAttribute("word")] 
    public WordsWord[] word { 
     get { 
      return this.wordField; 
     } 
     set { 
      this.wordField = 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 WordsWord { 

    private string engField; 

    private string engEndField; 

    private string partOfSpeechEngField; 

    private string hebField; 

    private string engInHebField; 

    private string partOfSpeechHebField; 

    private int dicNumberField; 

    private string arbField; 

    private string engInArbField; 

    /// <remarks/> 
    public string Eng { 
     get { 
      return this.engField; 
     } 
     set { 
      this.engField = value; 
     } 
    } 

    /// <remarks/> 
    public string EngEnd { 
     get { 
      return this.engEndField; 
     } 
     set { 
      this.engEndField = value; 
     } 
    } 

    /// <remarks/> 
    public string PartOfSpeechEng { 
     get { 
      return this.partOfSpeechEngField; 
     } 
     set { 
      this.partOfSpeechEngField = value; 
     } 
    } 

    /// <remarks/> 
    public string Heb { 
     get { 
      return this.hebField; 
     } 
     set { 
      this.hebField = value; 
     } 
    } 

    /// <remarks/> 
    public string EngInHeb { 
     get { 
      return this.engInHebField; 
     } 
     set { 
      this.engInHebField = value; 
     } 
    } 

    /// <remarks/> 
    public string PartOfSpeechHeb { 
     get { 
      return this.partOfSpeechHebField; 
     } 
     set { 
      this.partOfSpeechHebField = value; 
     } 
    } 

    /// <remarks/> 
    public int DicNumber { 
     get { 
      return this.dicNumberField; 
     } 
     set { 
      this.dicNumberField = value; 
     } 
    } 

    /// <remarks/> 
    public string Arb { 
     get { 
      return this.arbField; 
     } 
     set { 
      this.arbField = value; 
     } 
    } 

    /// <remarks/> 
    public string EngInArb { 
     get { 
      return this.engInArbField; 
     } 
     set { 
      this.engInArbField = value; 
     } 
    } 
} 

,你可以用它像這樣

XmlSerializer mySerializer = new XmlSerializer(typeof(Words)); 
FileStream myFileStream = new FileStream("path_to_xml",FileMode.Open); 

Words words = (Words)mySerializer.Deserialize(myFileStream);