title: XmlArray always initializes collections when deserializing XML File
body: <p>I cannot seem to deserialize a class without the collections (Values and Conditions) being initialized. My desire is to deserialize an XML file without the collections being initialized. Below is my class:</p>
  
  <pre><code>[Serializable]
  [XmlRoot(ElementName = "Swag")]
  public class SwagCollection : SerializableObservalbleCollection&lt;SwagItem&gt;
  {
  }
  
  [Serializable]
  public class SwagItem : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
      private string name;
      private ObservableCollection&lt;string&gt; values = null;
      private ObservableCollection&lt;Condition&gt; conditions = null;
  
      public SwagItem()
      {
          values = null;
          conditions = null;
      }
  
      [XmlAttribute]
      public string Name
      {
          get
          {
              return this.name;
          }
          set
          {
              this.name = value;
              OnPropertyChanged("Name");
          }
      }
  
      [XmlArray("Values"), XmlArrayItem("ValueItem")]
      public ObservableCollection&lt;string&gt; Values
      {
          get
          {
              return values;
          }
          set
          {
              values = value;
              OnPropertyChanged("Values");
          }
      }
  
      [XmlArray]
      public ObservableCollection&lt;Condition&gt; Conditions
      {
          get
          {
              return conditions;
          }
          set
          {
              conditions = value;
              OnPropertyChanged("Conditions");
          }
      }
  }
  </code></pre>
  
  <p>I have this saved as an embedded resource within my assembly:</p>
  
  <pre><code>&lt;Swag xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
     &lt;SwagItem Name="Bob"/&gt;
  &lt;/Swag &gt;
  </code></pre>
  
  <p>I have no problems retrieving the XML from the resources, using this method:</p>
  
  <pre><code>    private static T DeserializeFromResource&lt;T&gt;(string filename)
      {
          string result;
          using (Stream stream = typeof (MyTestAssembly).Assembly.
              GetManifestResourceStream("MyTestAssembly.Resources." + filename))
          {
              using (StreamReader sr = new StreamReader(stream))
              {
                  result = sr.ReadToEnd();
              }
          }
          XmlSerializer serializer = new XmlSerializer(typeof (T));
          TextReader textReader = new StringReader(result);
          return (T) serializer.Deserialize(textReader);
      }
  </code></pre>
  
  <p>I put a breakpoint right before the last line where we deserialize the TextReader, and the XML structure is still just one line, and does not have the null collections declared in the XML.</p>
  
  <p>However, when the deserialize method is called, the collections in my classes are getting initialized! (Despite being set to null in the fields and in the constructor). And unfortunately when I go to save my XML file, I'm left with empty collections:</p>
  
  <pre><code>&lt;Swag xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
     &lt;SwagItem Name="Bob"&gt;
        &lt;Values /&gt;
        &lt;Conditions /&gt;
     &lt;/SwagItem &gt;
  &lt;/Swag &gt;
  </code></pre>
  
  <p>I can't seem to get the Values and Conditions collections not to show in the XML if they are empty, despite setting their fields to null.</p>