Can we do XML Serialization for an object having collections?

Posted on 2008-10-18
Last Modified: 2010-08-05
Can we do XML serialization for an object having a collection?
If yes then, please provide me a small example how to do.
Question by:GouthamAnand
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

StasL earned 250 total points
ID: 22751265
Hi GouthamAnand,

The bottom line is you can serialize collections, as long as they are strongly typed.
Some collections may be serialized with no extra efforts, i.e. supported by the framework, while others will require some coding or googling.

As for the easy case sample is attached.
The more complex case (the generic dictionary for instance) can be handled using a custom serialization process.
You may refer to the following link for some samples:

List<string> someStringList = new List<string>();
XmlSerializer xs = new XmlSerializer(typeof(List<string>));
FileStream fs = new FileStream(@"c:\SerializedCollection.xml", 
xs.Serialize(fs, someStringList);
List<string> deserializedList = new List<string>();
FileStream fs1 = new FileStream(@"c:\SerializedCollection.xml", 
deserializedList = (List<string>)xs.Deserialize(fs1);

Open in new window


Assisted Solution

torrie01 earned 250 total points
ID: 22781210
I've found the easiest collection to serialize is an ArrayList, see the code below:
    public class ClassWithList
        public ClassWithList()
        // This attribute enables the ArrayList to be serialized:
        // Explicitly tell the serializer to expect the ClassWithItem class
        // so it can be properly written to XML from the collection:
        [System.Xml.Serialization.XmlArrayItem("Item", typeof(ClassWithItem))]
        public ArrayList ItemList { get; set; }

Open in new window


Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question