XML Serialize generic list of serializable objects

Can I serialize a generic list of serializable objects without having to specify their type.

Something like the intention behind the broken code below:

List<ISerializable> serializableList = new List<ISerializable>();

XmlSerializer xmlSerializer = new XmlSerializer(serializableList.GetType());


using (StreamWriter streamWriter = System.IO.File.CreateText(fileName))
    xmlSerializer.Serialize(streamWriter, serializableList);


For those who wanted to know detail: when I try to run this code, it errors on the XMLSerializer[…] line with:

Cannot serialize interface System.Runtime.Serialization.ISerializable.

If I change to List<object> I get "There was an error generating the XML document.". The InnerException detail is "{"The type System.Collections.Generic.List1[[Project1.Person, ConsoleFramework, Version=, Culture=neutral, PublicKeyToken=null]] may not be used in this context."}"

The person object is defined as follows:

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; }

The PersonList is just a List<Person> .

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.

I tried with List<IXmlSerializable> as well, but that fails with

System.Xml.Serialization.IXmlSerializable cannot be serialized because it does not have a parameterless constructor.

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.

Also thanks 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.

XML Serialize generic list of serializable objects