Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Can we do XML Serialization for an object having collections?

Posted on 2008-10-18
Medium Priority
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 1000 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 1000 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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

564 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