title: Deserialize random/unknown types with XmlSerializer
body: <p>I am using XmlSerializer to communicate with a service. This is not a regular SOAP service, it has its own XML object types. For example, I may ask for a <code>&lt;Capabilities&gt;</code> object, but it may return an <code>&lt;Exception&gt;</code>. So, in other words, I have to deal with random XML document types. I do however, know which types I have to deal with.</p>
  
  <p>What I am trying to do is to find a generic approach to serialize/deserialize these documents. The problem is that the XmlSerializer needs to know the type at creation stage.</p>
  
  <p>These are NOT encapsulated in a common root element, so making a base class and using the <code>[XmlInclude]</code> attribute does NOT work in this case:</p>
  
  <pre><code>[XmlInclude(typeof(Exception))]
  [XmlInclude(typeof(Capabilities))]
  public abstract class BaseClass
  {
    public BaseClass()
    {
      SchemaLocation = "test";
    }
  
    [XmlAttribute("schemaLocation")]
    public String SchemaLocation { get; set; }
  }
  
  [XmlRoot("Exception")]
  public class Exception : BaseClass
  {
    public Exception():base()
    {
    }
    [XmlElement]
    public String Message { set; get; }
  }
  
  [XmlRoot("Capabilities")]
  public class Capabilities : BaseClass
  {
    public Capabilities() : base()
    {}
    [XmlElement]
    public String ServiceName { set; get; }
  }
  </code></pre>
  
  <p>My solution so far is to probe the root element manually with the XmlReader, and then map it to the correct type before creating an XmlSerializer instance.</p>
  
  <p>Is there any better way of doing this ?</p>