unable to deserialize all elements of this xml

Few elements of this XML does not deserialize and but it does not throw any errors either.

<?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>

Below are my classes:

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

Now when I deserialize this XML, if Type is LiST, the WordList gets updated, e.g. here count for WordList will be 3 but if Type is RANDOM, WordList is 0 which should actually be 1 and not 0. Really don't know what could be the reason.

Jon Skeet
people
quotationmark

The problem is in your XML. Look at what it's like for your working case:

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

Now compare that with your broken case:

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

You've got no Words element in there - it should be:

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

If you can't change the XML, you may need to deserialize it by hand - which probably wouldn't be too hard.

As an aside, you might want to look into writing your automatically implemented properties on one line - it's a lot more compact to write

public string Name { get; set; }

than

public string Name
{
    get;
    set;
}

... and it's no harder to read, IMO.

people

See more on this question at Stackoverflow