2014-11-04 69 views
2

此XML的幾個元素不會反序列化,但它也不會引發任何錯誤。無法反序列化此xml的所有元素

<?xml version="1.0" encoding="utf-8"?> 
<TrialMWordsRecord xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <MyList> 
    <MWords> 
     <Id>0</Id> 
     <Name>ListMWords1</Name> 
     <Type>LIST</Type> 
     <MyOutput>true</MyOutput> 
     <WordsElements> 
     <Words> 
      <Name>ListMWords1</Name> 
      <Value>Apple</Value> 
      <Type>STRING</Type> 
     </Words> 
     <Words> 
      <Name>ListMWords1</Name> 
      <Value>Mango</Value> 
      <Type>STRING</Type> 
     </Words> 
     <Words> 
      <Name>ListMWords1</Name> 
      <Value>Chickoo</Value> 
      <Type>STRING</Type> 
     </Words> 
     </WordsElements> 
    </MWords> 
    <MWords> 
     <Id>1</Id> 
     <Type>RANDOM</Type> 
     <MyOutput>true</MyOutput> 
     <WordsElements> 
     <Name>Limit</Name> 
     <Value>3,8</Value> 
     <Type>NUMERIC</Type> 
     </WordsElements> 
    </MWords> 
    </TrialMWordsList> 
</MyListRecord> 

下面是我的課:

[Serializable()] 
[XmlRootAttribute("MyListRecord")] 
public class MyList 
{ 

    [XmlArray("MyList")] 
    public List<MWords> MWords 
    { 
     get; 
     set; 
    } 

    public static MyList Deserialize() 
    { 
     XmlSerializer deserializer = new XmlSerializer(typeof(MyList)); 
     TextReader textReader = new StreamReader(Application.StartupPath + "\\MyList.xml"); 

     MyList resultList = (MyList)deserializer.Deserialize(textReader); 

     textReader.Close(); 

     return resultList; 
    }  
} 

[Serializable()] 
public class MWords 
{ 

    public int Id 
    { 
     get; 
     set; 
    } 

    public MWordsType Type 
    { 
     get; 
     set; 
    } 

    public bool MyOutput 
    { 
     get; 
     set; 
    } 

    public string Requirement 
    { 
     get; 
     set; 
    } 

    [XmlArrayItem("WordsElements")] 
    public List<Words> WordList 
    { 
     get; 
     set; 
    } 

    public static MWords Deserialize() 
    { 
     XmlSerializer deserializer = new XmlSerializer(typeof(MWords)); 
     TextReader textReader = new StreamReader(Application.StartupPath + "\\MWords.xml"); 
     MWords mwords = (MWords)deserializer.Deserialize(textReader); 
     textReader.Close(); 
     return mwords; 
    } 
} 

public class Words 
{ 
    public string Value 
    { 
     get; 
     set; 
    } 

    public TYPE Type 
    { 
     get; 
     set; 
    } 

    public string Name 
    { 
     get; 
     set; 
    } 
} 

現在,當我反序列化這個XML,如果類型是列表,詞表得到更新,例如這裏計數WordList將是3,但如果類型是RANDOM,WordList是0,實際上應該是1而不是0. 真的不知道可能是什麼原因。爲類型的隨機<Words></Words>包裹元素WordsElements

+0

它給出了什麼異常? – user1291401 2014-11-04 08:15:14

回答

1

的問題是在你的XML。看看是什麼樣子的你的工作情況:

<WordsElements> 
    <Words> 
    <Name>ListMWords1</Name> 
    <Value>Apple</Value> 
    <Type>STRING</Type> 
    </Words> 
    <Words> 
    ... 
    </Words> 
    <Words> 
    ... 
    </Words> 
</WordsElements> 

現在比較,與你破碎的情況下:

<WordsElements> 
    <Name>Limit</Name> 
    <Value>3,8</Value> 
    <Type>NUMERIC</Type> 
</WordsElements> 

你有沒有Words因素在裏面 - 它應該是:

<WordsElements> 
    <Words> 
    <Name>Limit</Name> 
    <Value>3,8</Value> 
    <Type>NUMERIC</Type> 
    </Words> 
</WordsElements> 

如果你不能更改XML,你可能需要反序列化它 - 這可能不會太難。

順便說一句,你可能想看看在一行寫你自動實現的屬性 - 這是一個很多更緊湊寫

public string Name { get; set; } 

public string Name 
{ 
    get; 
    set; 
} 

...並不難讀,國際海事組織。

+0

感謝您的回答 – sia 2014-11-04 09:17:23

1

內容:

<MWords> 
    <Id>1</Id> 
    <Type>RANDOM</Type> 
    <MyOutput>true</MyOutput> 
    <WordsElements> 
    <Words> 
     <Name>Limit</Name> 
     <Value>3,8</Value> 
     <Type>NUMERIC</Type> 
    </Words> 
    </WordsElements> 
</MWords>