?
Solved

Can we do XML Serialization for an object having collections?

Posted on 2008-10-18
4
Medium Priority
?
223 Views
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.
0
Comment
Question by:GouthamAnand
[X]
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
4 Comments
 
LVL 1

Accepted Solution

by:
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:

http://blogs.msdn.com/psheill/archive/2005/04/09/406823.aspx

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

Open in new window

0
 
LVL 3

Assisted Solution

by:torrie01
torrie01 earned 1000 total points
ID: 22781210
I've found the easiest collection to serialize is an ArrayList, see the code below:
    [Serializable()]
    public class ClassWithList
    {
        public ClassWithList()
        {
            
        }
 
        
        // This attribute enables the ArrayList to be serialized:
        [System.Xml.Serialization.XmlArray("ListOfItems")]
        // 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

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 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