title: Why my ObservableCollection serialization doesn't work?
body: <p>I'm trying to serialize and deserialize this ObservableCollection:</p>
  
  <pre><code>public class DataCollection : ObservableCollection&lt;Data&gt;
  {
  }
  
  public class Data : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
  
      private bool? _enabled;
  
      public string Name { get; set; }
      public bool? Enabled 
      {
          get { return _enabled; }
          set { _enabled = value; NotifyPropertyChanged("Enabled"); }
      }
  
      public Data(string name, bool? enabled)
      {
          this.ScriptName = name;
          this.Enabled = enabled;
      }
  
      private void NotifyPropertyChanged(string property)
      {
          if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs(property));
      }
  }
  </code></pre>
  
  <p>Using this class (as in the <a href="https://stackoverflow.com/questions/1194931/what-is-the-easiest-way-to-save-an-observable-collectin-of-object-to-an-xml-file">What is the easiest way to save an Observable collectin of object to an XML file?</a> example):</p>
  
  <pre><code>class UserPreferences
  {
      private DataCollection _dataLst;
      private const string _dataLstFileName = "Data.xml";
  
      public DataCollection DataLst { get { return _dataLst; } set { _dataLst = value; } }
  
      public UserPreferences()
      {
          Load();
      }
  
      public void Load()
      {
          if (File.Exists(_dataLstFileName))
          {
              using (var reader = new StreamReader(_dataLstFileName))
              {
                  var xs = new XmlSerializer(typeof(DataCollection));
                  _dataLst = (DataCollection) xs.Deserialize(reader);
              }
          }
          else
          {
              _dataLst = new DataCollection();
          }
      }
  
      public void Save()
      {
          using (var writer = new StreamWriter(_dataLstFileName))
          {
              var xs = new XmlSerializer(typeof(DataCollection));
              xs.Serialize(writer, _dataLst);
          }
      }
  }
  </code></pre>
  
  <p>And here is how I call it from my app:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      private DataCollection DataLst;
  
      ...
  
      private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
      {
          var userPrefs = new UserPreferences();
  
          userPrefs.DataLst = DataLst; // DataList isn't null and contains correct data
          userPrefs.Save(); 
      }
  }
  </code></pre>
  
  <p>But it creates empty file and hangs up (even without exceptions, just app window becomes black and doesn't response) in the line</p>
  
  <pre><code>var xs = new XmlSerializer(typeof(DataCollection));  
  </code></pre>