title: C# Serialize XML to Object
body: <p>I am trying to serializer an XML into an object. I am however, having some trouble. I have gone through maybe postings online regarding an answer, but I haven't been able to figure it out. Please let me explain:</p>
  
  <p>I have the below XML that I want to serialise:</p>
  
  <pre><code>&lt;Import_RootObject&gt;
   &lt;Organizations&gt;
    &lt;Import_Organization OrgNr="xxxx"&gt;
     &lt;Events&gt;
      &lt;Import_Event StartTime="2012-01-01 09:00:00" EndTime="2012-01-02 12:00:00"&gt;
      &lt;Players&gt;
       &lt;Import_Player PersonNummer="1111" /&gt;
       &lt;Import_Player PersonNummer="2222" /&gt;
       &lt;Import_Player PersonNummer="3333" /&gt;
       &lt;Import_Player PersonNummer="4444" /&gt;
      &lt;/Players&gt;
     &lt;/Import_Event&gt;
    &lt;/Events&gt;
   &lt;/Import_Organization&gt;
  &lt;/Organizations&gt;
  </code></pre>
  
  <p></p>
  
  <p>Im using four classes to capture this XML:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Xml.Serialization;
  
  [XmlRoot("Import_RootObject")]
  public class Import_RootObject
  {
      [XmlArray("organizations")]
      [XmlArrayItem("organizations")]
      public List&lt;Import_Organization&gt; Organizations { get; set; }
  }
  
  
  
  using System;
  using System.Collections.Generic;
  using System.Xml.Serialization;
  
  public class Import_Organization
  {
      [XmlAttribute("orgnr")]
      public string OrgNr { get; set; }
  
      [XmlArray("events")]
      [XmlArrayItem("events")]
      public List&lt;Import_Event&gt; Events { get; set; }
  
  }
  
  
  
  using System;
  using System.Collections.Generic;
  using System.Xml.Serialization;
  
  public class Import_Event
  {
      [XmlAttribute("starttime")]
      public DateTime StartTime { get; set; }
      [XmlAttribute("endtime")]
      public DateTime EndTime { get; set; }
  
      [XmlArray("players")]
      [XmlArrayItem("players")]
      public List&lt;Import_Player&gt; Players { get; set; }
  
  }
  
  
  
  using System;
  using System.Collections.Generic;
  using System.Xml.Serialization;
  
  public class Import_Player
  {
      [XmlAttribute]
      public string PersonNummer { get; set; }
  
  }
  </code></pre>
  
  <p>The code I use to deserialize is:</p>
  
  <pre><code>XmlSerializer serializer = new XmlSerializer(typeof(Import_Organization));
  Import_RootObject ei = (Import_RootObject)serializer.Deserialize(new StringReader(sb.ToString()));
  </code></pre>
  
  <p>And the error I'm getting is:</p>
  
  <pre><code>There is an error in XML document (1, 2).
  &lt;Import_RootObject xmlns=''&gt; was not expected.
  </code></pre>
  
  <p>Does anyone know what I'm missing here? Hope someone can help out!</p>
  
  <p>Regards,</p>
  
  <p>Bob</p>