title: How do I deserialize XML without knowing the type beforehand?
body: <p>Say I have a couple of basic objects like so:</p>
  
  <pre><code>[Serializable]
  public class Base
  {
  
      public string Property1 { get; set; }
      public int Property2 { get; set; }
  }
  
  [Serializable]
  public class Sub: Base
  {
      public List&lt;string&gt; Property3 { get; set; }
  
      public Sub():base()
      {
          Property3 = new List&lt;string&gt;();
      }        
  }
  </code></pre>
  
  <p>And I serialize them like so:</p>
  
  <pre><code>Sub s = new Sub {Property1 = "subtest", Property2 = 1000};
  s.Property3.Add("item 1");
  s.Property3.Add("item 2");
  
  XmlSerializer sFormater = new XmlSerializer(typeof(Sub));
  using (FileStream fStream = new FileStream("SubData.xml", 
      FileMode.Create, FileAccess.Write, FileShare.None))
  {
      sFormater.Serialize(fStream, s);
  }
  </code></pre>
  
  <p>How can I deserialize them, so that I get back the correct class?</p>
  
  <p>As in, I'd want something like this </p>
  
  <pre><code>XmlSerializer bFormater = new XmlSerializer(typeof (Base));
  Base newBase;
  using (FileStream fStream = new FileStream("BaseData.xml", 
      FileMode.Open, FileAccess.Read, FileShare.Read))
  {
      newBase = (Base) bFormater.Deserialize(fStream);
  }
  </code></pre>
  
  <p>Except I'd be able to pass it an XML file for any class that descends from Base and the correct class would be created.</p>
  
  <p>I'm thinking I could read the name of the root node of the XML and use a switch statement to create the correct XmlSerializer, but I was wondering if there was a simpler way.</p>