title: Deserialize XML document to C# class, with multiple root (xmlns) namespaces
body: <p>I'm trying to deserialize a XML document, with different root namespaces,  to a C# class.  </p>
  
  <p>In short, I want to deserialize multiple versions of a similar xml document like so:  </p>
  
  <pre><code>&lt;IndexRoot Code="0664" xmlns="http://tempuri/2012/1.0"&gt;
      &lt;Name&gt;Foo&lt;/Name&gt;
      &lt;Color&gt;blue&lt;/Color&gt;
      ...
  &lt;/IndexRoot&gt;
  
  
  &lt;IndexRoot Code="0678" xmlns="http://tempuri/2012/2.0"&gt;
      &lt;Name&gt;Bar&lt;/Name&gt;
      &lt;Character&gt;Smurf&lt;/Character&gt;
  &lt;/IndexRoot&gt;
  </code></pre>
  
  <p>Each version can obviously have different elements below it, and whilst most elements are the same there are some differences. In the sample above the Name attribute is available in each version, while the Color/Character are unique to each version.</p>
  
  <p>Ideally, I want to abstract this to a simple function that gives me a reflected concrete class. Like so:</p>
  
  <pre><code>public IndexRoot Get(string fileName) {
      var doc = XDocument.Load(fileName);
      return xmlSerializer.Deserialize&lt;IndexRoot&gt;(doc);   
  }
  </code></pre>
  
  <p>In my current setup this fails, because it is required to provide a single namespace on the root element for the deserializer to work:</p>
  
  <pre><code>[Serializable, XmlRoot(ElementName = "IndexRoot", Namespace = "http://tempuri/2012/2.0")]
  public class IndexRoot
  {
      [XmlAttribute("Code")]
      public string Code { get; set; }
  
      [XmlElement(ElementName = "Name")]
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>As you can see, the hardcoded namespace will work for 2.0 versions but will fail for other versions with the exception: <em>"IndexRoot xmlns='<a href="http://tempuri/2012/1.0" rel="nofollow">http://tempuri/2012/1.0</a>' was not expected."</em></p>
  
  <p><strong>The question:</strong> how can I deserialize the XML to a C# object, taking the multiple root namespaces into consideration? </p>
  
  <p>Ideally, this would be reflected to a concrete type per version. But I'll even settle for getting a "base class" with the common, shared properties. Either way, I'm currently stuck with the current hardcoded Namespace value on the [XmlRoot].</p>
  
  <p>I have tried:</p>
  
  <ol>
  <li>Adding duplicate [XmlRoot] attributes (which is not supported)</li>
  <li>Create a base class (BaseIndexRoot), deriving two instances from it and decorating these derivates with the [XmlRoot] attribute (same "was not expected" error)</li>
  <li>Removing the Namespace all together also results in the "was not expected" error</li>
  </ol>