title: IXmlSerializable and inheritance
body: <p>Let's say that I have two classes, a base class and a derived class. They are fairly simple classes, and mainly just act as data structures (with the derived class obviously being slightly more complex).</p>
  
  <pre><code>public class BaseUserSession
  {
      // ... Various properties ...
  }
  
  public class DerivedUserSession : BaseUserSession
  {
      // ... Even more properties ...
  }
  </code></pre>
  
  <p>These classes need to be serialized into XML. Now, this can be done simply by specifying <code>[Serializable]</code> above the class declarations, and the default XML serializer works well.</p>
  
  <p>In reality, however, there are multiple classes that inherit from the base class. My goal is to be able to have the serialized XML of any of the derived classes be consumable by the base class.</p>
  
  <p>I have tried two methods, neither of which work currently. First, using the native XML serializer and some control attributes within the base class. The generated XML looks something like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
  &lt;DerivedUserSession xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SSO="www.mywebsite.com"&gt;
      &lt;SSO:Id /&gt;
      &lt;SSO:Authenticated&gt;false&lt;/SSO:Authenticated&gt;
      &lt;SSO:Role /&gt;
      &lt;EmailAddress /&gt;
      &lt;FullName /&gt;
      &lt;Street /&gt;
      &lt;City /&gt;
      &lt;State /&gt;
      &lt;Zip /&gt;
      &lt;DayPhone /&gt;
      &lt;EveningPhone /&gt;
      &lt;ErrorMessage /&gt;
      &lt;Birthdate&gt;0001-01-01T00:00:00&lt;/Birthdate&gt;
      &lt;CurrentControl&gt;0&lt;/CurrentControl&gt;
  &lt;/DerivedUserSession&gt;
  </code></pre>
  
  <p>The tags that are prefixed with <code>SSO</code> are the ones that were inherited from the base class. But when I attempt to deserialize this into a <code>BaseUserSession</code> object, it throws an exception: <code>System.InvalidOperationException: &lt;DerivedUserSession xmlns=''&gt; was not expected.</code></p>
  
  <p>I also tried making the base class <code>IXmlSerializable</code>, and reading and writing all of the XML manually. However, the derived classes fail to write out their properties, and I really don't want to implement <code>IXmlSerializable</code> for every class.</p>
  
  <p>Is there an easier way to do this?</p>
  
  <p><strong>=Edit=</strong></p>
  
  <p>I have finally implemented a fairly ugly method to accomplish this. The two classes inherit from the base, and they all implement <code>IXmlSerializable</code>. Each class (including the base) writes out its field values (painstakingly...) into its own XML hierarchy using reflection, as well as reads those values back. The first values written are always from the base class.</p>
  
  <p>Each application also includes code that manages the serialization into XML and back, providing an easy interface. The session itself doesn't do anything more now than simply accept the serialized XML and store it under a common key. The serialization into and from XML is all handled by the application, and a common <code>XmlRootAttribute</code> is used in all applications to bypass the <code>InvalidOperationException</code>.</p>
  
  <p>So now, when another application tries to deserialize XML that was initially created in a different application, it manages to read through the common base class values before it hits an unrecognized element, at which point it goes and fetches the needed values elsewhere, which is possible because it will already have the common base values.</p>
  
  <p>I need to read up on serialization principles...</p>