title: Serializing ObservableCollection
body: <p>I want to save an ObservableCollection that contains "Number" elements to the IsolatedStorage on the phone. I'm calling the SaveHistory() and LoadHistory() methods of my FileAcces class in App.xaml.cs whenever the app closes, activates, launches etc. I have a global variable defined in App.xaml.cs called <em>list</em> (ObservableCollection)</p>
  
  <p>When debugging I'm recieving the error <strong>InvalidOperationException was unhandled</strong> (<em>There is an error in XML document (3, 4).</em>) on the line that says</p>
  
  <p><code>App.list = (ObservableCollection&lt;Number&gt;)serializer.Deserialize(stream);</code> (in LoadHistory())</p>
  
  <pre><code>public class FileAccess
  {
      public static void SaveHistory()
      {
              using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
              {
                  using (IsolatedStorageFileStream stream = store.CreateFile("History.xml"))
                  {
                      XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection&lt;Number&gt;));
                      serializer.Serialize(stream, App.list);
                  }
              }
      }
  
      public static void LoadHistory()
      {
          using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
          {
              //Make the file if it doesn't exist.
              if (!store.FileExists("History.xml"))
              {
                  IsolatedStorageFileStream file = store.CreateFile("History.xml");
                  //Dispose of the stream automatically created when creating the file. 
                  file.Close();
                  file.Dispose();
              }
              using (IsolatedStorageFileStream stream = store.OpenFile("History.xml", System.IO.FileMode.Open))
              {
                  //If the file isnt empty, then deserialize the xml into Settings objects and load them into the database.
                  if (stream.Length &gt; 0)
                  {
                      XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection&lt;Number&gt;));
                      App.list = (ObservableCollection&lt;Number&gt;)serializer.Deserialize(stream);
                  }
                  else
                  {
                      //If the file is empty, create a new ObservableCollection of Settings class type.
                      App.list = new ObservableCollection&lt;Number&gt;();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>The contents of the XML file</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;ArrayOfNumber xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;Number&gt;
          &lt;numbers&gt;23&lt;/numbers&gt;
      &lt;/Number&gt;
      &lt;Number&gt;
          &lt;numbers&gt;50&lt;/numbers&gt;
      &lt;/Number&gt;
      &lt;Number&gt;
          &lt;numbers&gt;14&lt;/numbers&gt;
      &lt;/Number&gt;
      &lt;Number&gt;
          &lt;numbers&gt;76&lt;/numbers&gt;
      &lt;/Number&gt;
      &lt;Number&gt;
          &lt;numbers&gt;21&lt;/numbers&gt;
      &lt;/Number&gt;
  &lt;/ArrayOfNumber&gt;
  </code></pre>
  
  <p>According to <a href="http://robtiffany.com/tag/observablecollection" rel="nofollow">robtiffany</a>, in the article posted on the 5:th of November, serializing an observablecollection this way should work. What am I doing wrong?</p>