Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3992126
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:43:41+00:00 2026-05-20T06:43:41+00:00

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

  • 0

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());

serializableList.Add((ISerializable)PersonList);

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

Edit:

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=1.0.0.0, Culture=neutral, PublicKeyToken=null]] may not be used in this context."}"

The person object is defined as follows:

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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:43:42+00:00Added an answer on May 20, 2026 at 6:43 am

    I have an solution for a generic List<> with dynamic binded items.

    class PersonalList it’s the root element

    [XmlRoot("PersonenListe")]
    [XmlInclude(typeof(Person))] // include type class Person
    public class PersonalList
    {
        [XmlArray("PersonenArray")]
        [XmlArrayItem("PersonObjekt")]
        public List<Person> Persons = new List<Person>();
    
        [XmlElement("Listname")]
        public string Listname { get; set; }
    
        // Konstruktoren 
        public PersonalList() { }
    
        public PersonalList(string name)
        {
            this.Listname = name;
        }
    
        public void AddPerson(Person person)
        {
            Persons.Add(person);
        }
    }
    

    class Person it’s an single list element

    [XmlType("Person")] // define Type
    [XmlInclude(typeof(SpecialPerson)), XmlInclude(typeof(SuperPerson))]  
            // include type class SpecialPerson and class SuperPerson
    public class Person
    {
        [XmlAttribute("PersID", DataType = "string")]
        public string ID { get; set; }
    
        [XmlElement("Name")]
        public string Name { get; set; }
    
        [XmlElement("City")]
        public string City { get; set; }
    
        [XmlElement("Age")]
        public int Age { get; set; }
    
        // Konstruktoren 
        public Person() { }
    
        public Person(string name, string city, int age, string id)
        {
            this.Name = name;
            this.City = city;
            this.Age = age;
            this.ID = id;
        }
    }
    

    class SpecialPerson inherits Person

    [XmlType("SpecialPerson")] // define Type
    public class SpecialPerson : Person
    {
        [XmlElement("SpecialInterests")]
        public string Interests { get; set; }
    
        public SpecialPerson() { }
    
        public SpecialPerson(string name, string city, int age, string id, string interests)
        {
            this.Name = name;
            this.City = city;
            this.Age = age;
            this.ID = id;
            this.Interests = interests;
        }
    }
    

    class SuperPerson inherits Person

    [XmlType("SuperPerson")] // define Type
    public class SuperPerson : Person
    {
        [XmlArray("Skills")]
        [XmlArrayItem("Skill")]
        public List<String> Skills { get; set; }
    
        [XmlElement("Alias")]
        public string Alias { get; set; }
    
        public SuperPerson() 
        {
            Skills = new List<String>();
        }
    
        public SuperPerson(string name, string city, int age, string id, string[] skills, string alias)
        {
            Skills = new List<String>();
    
            this.Name = name;
            this.City = city;
            this.Age = age;
            this.ID = id;
            foreach (string item in skills)
            {
                this.Skills.Add(item);   
            }
            this.Alias = alias;
        }
    }
    

    and the main test Source

    static void Main(string[] args)
    {
        PersonalList personen = new PersonalList(); 
        personen.Listname = "Friends";
    
        // normal person
        Person normPerson = new Person();
        normPerson.ID = "0";
        normPerson.Name = "Max Man";
        normPerson.City = "Capitol City";
        normPerson.Age = 33;
    
        // special person
        SpecialPerson specPerson = new SpecialPerson();
        specPerson.ID = "1";
        specPerson.Name = "Albert Einstein";
        specPerson.City = "Ulm";
        specPerson.Age = 36;
        specPerson.Interests = "Physics";
    
        // super person
        SuperPerson supPerson = new SuperPerson();
        supPerson.ID = "2";
        supPerson.Name = "Superman";
        supPerson.Alias = "Clark Kent";
        supPerson.City = "Metropolis";
        supPerson.Age = int.MaxValue;
        supPerson.Skills.Add("fly");
        supPerson.Skills.Add("strong");
    
        // Add Persons
        personen.AddPerson(normPerson);
        personen.AddPerson(specPerson);
        personen.AddPerson(supPerson);
    
        // Serialize 
        Type[] personTypes = { typeof(Person), typeof(SpecialPerson), typeof(SuperPerson) };
        XmlSerializer serializer = new XmlSerializer(typeof(PersonalList), personTypes); 
        FileStream fs = new FileStream("Personenliste.xml", FileMode.Create); 
        serializer.Serialize(fs, personen); 
        fs.Close(); 
        personen = null;
    
        // Deserialize 
        fs = new FileStream("Personenliste.xml", FileMode.Open); 
        personen = (PersonalList)serializer.Deserialize(fs); 
        serializer.Serialize(Console.Out, personen);
        Console.ReadLine();
    }
    

    Important is the definition and includes of the diffrent types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object I can serialize to XML without a problem. However when
What are different ways in which we can XML serialize multiple objects of same/different
Why won't XMLSerializer process my generic list? Sub Main() Serializing() End Sub <System.Serializable()> _
I'm running into an issue using System.Runtime.Serialization.Json.DataContractJsonSerializer to serialize a List<T> of proxied objects.
How can I serialize a string from XML into a class property of type
I am looking for a tool that can serialize and/or transform SQL Result Sets
Can all .NET exception objects be serialized?
Can somebody point me to a resource that explains how to go about having
Can you cast a List<int> to List<string> somehow? I know I could loop through
Can anyone tell me how I can display a status message like 12 seconds

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.