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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
dynamic menu in c# 11 30
Events in static methods 3 31
Unable  to create new object 9 28
Open a word document 23 19
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now