title: XmlSerializer exception when deserializing derived class (<Derived xmlns=''> was not expected)
body: <p>I am trying to serialize and deserialize a hierarchy of classes using XmlSerializer. The serialization works fine, but when I try to deserialize I get this exception:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: There is an error in XML document (2, 2). ---> System.InvalidOperationException: <code>&lt;Derived xmlns=''&gt;</code> was not expected.</p>
  </blockquote>
  
  <p>This is the xml that I am trying to deserialize:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;Derived xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;BaseStr&gt;Base&lt;/BaseStr&gt;
    &lt;DerivedStr&gt;Derived&lt;/DerivedStr&gt;
  &lt;/Derived&gt;
  </code></pre>
  
  <p>This is the code that I am using:</p>
  
  <pre class="lang-csharp prettyprint-override"><code>using System;
  using System.IO;
  using System.Text;
  using System.Xml;
  using System.Xml.Serialization;
  
  [XmlInclude(typeof(Derived))]
  public abstract class Base 
  {
      public string BaseStr { get; set; }
  }
  
  public class Derived : Base
  {
      public string DerivedStr { get; set; }
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          try
          {
              XmlSerializer serializer = new XmlSerializer(typeof(Derived));
              MemoryStream ms = new MemoryStream();
              serializer.Serialize(ms, new Derived() { DerivedStr = "Derived", BaseStr = "Base" });
  
              Console.WriteLine(Encoding.ASCII.GetChars(ms.ToArray()));
  
              ms.Position = 0;
  
              XmlSerializer deserializer = new XmlSerializer(typeof(Base));
              Base b = (Base)deserializer.Deserialize(ms);
              Console.WriteLine(b.GetType().Name);
          }
          catch(Exception ex)
          {
              Console.WriteLine();
              Console.WriteLine(ex.ToString());
          }
      }
  }
  </code></pre>
  
  <p>Why does the deserialization not work? How can I get it to work?</p>