title: XML Serialize generic list of serializable objects
body: <p>Can I serialize a generic list of serializable objects without having to specify their type.</p>
  
  <p>Something like the intention behind the broken code below:</p>
  
  <pre><code>List&lt;ISerializable&gt; serializableList = new List&lt;ISerializable&gt;();
  
  XmlSerializer xmlSerializer = new XmlSerializer(serializableList.GetType());
  
  serializableList.Add((ISerializable)PersonList);
  
  using (StreamWriter streamWriter = System.IO.File.CreateText(fileName))
  {
      xmlSerializer.Serialize(streamWriter, serializableList);
  }
  </code></pre>
  
  <p>Edit:</p>
  
  <p>For those who wanted to know detail: when I try to run this code, it errors on the XMLSerializer[...] line with:</p>
  
  <p>Cannot serialize interface System.Runtime.Serialization.ISerializable.</p>
  
  <p>If I change to <code>List&lt;object&gt;</code> I get <code>"There was an error generating the XML document."</code>. The InnerException detail is <code>"{"The type System.Collections.Generic.List1[[Project1.Person, ConsoleFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] may not be used in this context."}"</code></p>
  
  <p>The person object is defined as follows:</p>
  
  <pre><code>[XmlRoot("Person")]
  public class Person
  {
      string _firstName = String.Empty;
      string _lastName = String.Empty;
  
      private Person()
      {
      }
  
      public Person(string lastName, string firstName)
      {
          _lastName = lastName;
          _firstName = firstName;
      }
  
      [XmlAttribute(DataType = "string", AttributeName = "LastName")]
      public string LastName
      {
          get { return _lastName; }
          set { _lastName = value; }
      }
  
      [XmlAttribute(DataType = "string", AttributeName = "FirstName")]
      public string FirstName
      {
          get { return _firstName; }
          set { _firstName = value; }
      }
  }
  </code></pre>
  
  <p>The PersonList is just a <code>List&lt;Person&gt;</code> .</p>
  
  <p>This is just for testing though, so didn't feel the details were too important. The key is I have one or more different objects, all of which are serializable. I want to serialize them all to one file. I thought the easiest way to do that would be to put them in a generic list and serialize the list in one go. But this doesn't work.</p>
  
  <p>I tried with <code>List&lt;IXmlSerializable&gt;</code> as well, but that fails with</p>
  
  <p><code>System.Xml.Serialization.IXmlSerializable cannot be serialized because it does not have a parameterless constructor.</code></p>
  
  <p>Sorry for the lack of detail, but I am a beginner at this and don't know what detail is required. It would be helpful if people asking for more detail tried to respond in a way that would leave me understanding what details are required, or a basic answer outlining possible directions.</p>
  
  <p>Also <strong>thanks</strong> to the two answers I've got so far - I could have spent a lot more time reading without getting these ideas. It's amazing how helpful people are on this site.</p>