title: How to serialize a derived class as its base class
body: <p>I have a derived class that adds only methods to a base class. How can one serialize the derived class so that it matches the serialization of the base class?
  i.e. The serialized xml of the derived class should look like:</p>
  
  <pre><code>&lt;BaseClass&gt;
    ...
  &lt;/BaseClass&gt;
  </code></pre>
  
  <p>e.g.
  The following will throw an <code>InvalidOperationException</code></p>
  
  <blockquote>
    <p>The type DerivedClass was not expected. Use the XmlInclude or
    SoapInclude attribute to specify types that are not known statically.</p>
  </blockquote>
  
  <pre><code>Class BaseClass {}
  
  Class DerivedClass : BaseClass {}
  
  DerivedClass derived = new DerivedClass();
  
  StreamWriter stream = new StreamWriter("output file path");
  XmlSerializer serializer = new XmlSerializer(GetType(BaseClass));
  serializer(stream, derived);
  </code></pre>