title: Deserialize XML to ObservableCollection
body: <p>Using VS 2012 and .NET 4.0, I have the following code which correctly deserializes an XML file:</p>
  
  <p>XML File:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt; 
  &lt;Codes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:xsd="http://www.w3.org/2001/XMLSchema" &gt;
    &lt;Code&gt;
      &lt;AccpacCode&gt;CA9998&lt;/AccpacCode&gt;
      &lt;LAC&gt;90699999999999999&lt;/LAC&gt;
      &lt;SCSCode&gt;SDI&lt;/SCSCode&gt;
    &lt;/Code&gt;
    &lt;Code&gt;
      &lt;AccpacCode&gt;ORWC&lt;/AccpacCode&gt;
      &lt;LAC&gt;94199999999999999&lt;/LAC&gt;
      &lt;SCSCode&gt;WC&lt;/SCSCode&gt;
    &lt;/Code&gt;
    &lt;Code&gt;
      &lt;AccpacCode&gt;AK9999&lt;/AccpacCode&gt;
      &lt;LAC&gt;90299999999999999&lt;/LAC&gt;
      &lt;SCSCode&gt;UI&lt;/SCSCode&gt;
      &lt;ParentEmployerAccpacCode&gt;AKSUTA&lt;/ParentEmployerAccpacCode&gt;
    &lt;/Code&gt;
  &lt;Codes&gt;
  </code></pre>
  
  <p>XSD File:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xs:schema id="Codes" xmlns="SerializeObservableCollection" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"&gt;
    &lt;xs:element name="Codes" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"&gt;
      &lt;xs:complexType&gt;
        &lt;xs:choice minOccurs="0" maxOccurs="unbounded"&gt;
          &lt;xs:element name="Code"&gt;
            &lt;xs:complexType&gt;
              &lt;xs:sequence&gt;
                &lt;xs:element name="AccpacCode" type="xs:string" minOccurs="0" /&gt;
                &lt;xs:element name="LAC" type="xs:string" minOccurs="0" /&gt;
                &lt;xs:element name="SCSCode" type="xs:string" minOccurs="0" /&gt;
                &lt;xs:element name="ParentEmployerAccpacCode" type="xs:string" minOccurs="0" /&gt;
              &lt;/xs:sequence&gt;
            &lt;/xs:complexType&gt;
          &lt;/xs:element&gt;
        &lt;/xs:choice&gt;
      &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <p>C# Code:</p>
  
  <pre><code>try
  {
      XmlSerializer _serializer = new XmlSerializer(typeof(Codes));
  
      // A file stream is used to read the XML file into the ObservableCollection
      using (StreamReader _reader = new StreamReader(@"LocalCodes.xml"))
      {
          var codes = _serializer.Deserialize(_reader) as Codes;
  
      }                
  }
  catch (Exception ex)
  {
      // Catch exceptions here
  }
  </code></pre>
  
  <p>I would like to put the results of the deserialization into an ObservableCollection and have found examples that say the following should work:</p>
  
  <pre><code>ObservableCollection&lt;Codes&gt; CodeCollection;
  </code></pre>
  
  <p>...</p>
  
  <pre><code>try
  {
      XmlSerializer _serializer = new XmlSerializer(typeof(ObservableCollection&lt;Codes&gt;));
  
      // A file stream is used to read the XML file into the ObservableCollection
      using (StreamReader _reader = new StreamReader(@"LocalCodes.xml"))
      {
          CodeCollection = _serializer.Deserialize(_reader) as ObservableCollection&lt;Codes&gt;;
  
      }                
  }
  </code></pre>
  
  <p>When I run this code I get an error message of <code>"There is an error in XML document (2, 2)."</code> and an inner exception of <code>"&lt;Codes xmlns=''&gt; was not expected."</code>  I have seen mention of needing a default constructor to make this work and the Codes.cs class does have one (it is the file generated by VS 2012 by the XSD file).  here is a snippet of the Codes.cs file:</p>
  
  <pre><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //     This code was generated by a tool.
  //     Runtime Version:4.0.30319.18051
  //
  //     Changes to this file may cause incorrect behavior and will be lost if
  //     the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  #pragma warning disable 1591
  
  namespace SerializeObservableCollection {
  
  
      /// &lt;summary&gt;
      ///Represents a strongly typed in-memory cache of data.
      ///&lt;/summary&gt;
      [global::System.Serializable()]
      [global::System.ComponentModel.DesignerCategoryAttribute("code")]
      [global::System.ComponentModel.ToolboxItem(true)]
      [global::System.Xml.Serialization.XmlSchemaProviderAttribute("GetTypedDataSetSchema")]
      [global::System.Xml.Serialization.XmlRootAttribute("Codes")]
      [global::System.ComponentModel.Design.HelpKeywordAttribute("vs.data.DataSet")]
      public partial class Codes : global::System.Data.DataSet {
  
          private CodeDataTable tableCode;
  
          private global::System.Data.SchemaSerializationMode _schemaSerializationMode = global::System.Data.SchemaSerializationMode.IncludeSchema;
  
          [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
          [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
          public Codes() {
              this.BeginInit();
              this.InitClass();
              global::System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new global::System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
              base.Tables.CollectionChanged += schemaChangedHandler;
              base.Relations.CollectionChanged += schemaChangedHandler;
              this.EndInit();
          }
  </code></pre>
  
  <p>What do I need to change/fix to get this to work and populate the ObservableCollection?</p>