title: XML deserialization: working with list
body: <p>I have following XML file format. I want to deserialize following into object, but it does not work. I get problem while deserializing "Field". </p>
  
  <pre><code>&lt;ExportDefinition&gt;
    &lt;DestinationDir&gt;C:\Export\&lt;/DestinationDir&gt;
    &lt;LevelID&gt;102&lt;/LevelID&gt;
    &lt;Metadata&gt;
      &lt;Field&gt;
          &lt;Source&gt;CreateDate&lt;/Source&gt;
          &lt;DataType&gt;DateTime&lt;/DataType&gt;
      &lt;Field&gt;
      &lt;Field&gt;
          &lt;Source&gt;UserName&lt;/Source&gt;
          &lt;DataType&gt;Text&lt;/DataType&gt;
      &lt;Field&gt;
    &lt;/Metadata&gt;
  &lt;/ExportDefinition&gt;
  
  
  
  static void Main(string[] args)
  {
     string xmlFile = @"C:\cobra_exp_def.xml";
     FileStream ReadFileStream = new FileStream(xmlFile, FileMode.Open, FileAccess.Read, FileShare.Read);
  
    XmlSerializer SerializerObj = new XmlSerializer(typeof(ExportDefinition));
    ExportDefinition LoadedObj = (ExportDefinition)SerializerObj.Deserialize(ReadFileStream);
  
    ReadFileStream.Close();
  }
  
  public class ExportDefinition
  {
     [XmlElement]
     public string DestinationDir { get; set; }
  
     [XmlElement]
     public string LevelID { get; set; }
  }
  
  public class Metadata
  {
     private List&lt;Field&gt; list = new List&lt;Field&gt;();
  
     [XmlElement]
     public List&lt;Field&gt; Metadata { get { return list; } set { list = value; } }
  }
  
  public class Field
  {
    [XmlElement]
    public string Source { get; set; }
  
    [XmlElement]
    public string DataType { get; set; }
  }
  </code></pre>
  
  <p><H1>Solution</H1>
  Finally have managed it to work with following:</p>
  
  <pre><code>[XmlArray("Metadata"), XmlArrayItem("Field", typeof(Field))]
          public List&lt;Field&gt; Metadata
          {
              get { return (_fields); }
              set { _fields = value; }
          } 
  
          private List&lt;Field&gt; _fields = new List&lt;Field&gt;();
  </code></pre>