title: Using generics with XmlSerializer
body: <p>When using XML serialization in C#, I use code like this:</p>
  
  <pre><code>public MyObject LoadData()
  {
      XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyObject));
      using (TextReader reader = new StreamReader(settingsFileName))
      {
          return (MyObject)xmlSerializer.Deserialize(reader);
      }
  }
  </code></pre>
  
  <p>(and similar code for deserialization).</p>
  
  <p>It requires casting and is not really nice. Is there a way, directly in .NET Framework, to use generics with serialization? That is to say to write something like:</p>
  
  <pre><code>public MyObject LoadData()
  {
      // Generics here.
      XmlSerializer&lt;MyObject&gt; xmlSerializer = new XmlSerializer();
      using (TextReader reader = new StreamReader(settingsFileName))
      {
          // No casts nevermore.
          return xmlSerializer.Deserialize(reader);
      }
  }
  </code></pre>