473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Serialize CollectionBase derived List to XML file

Hi,

I have a list which is derived from CollectionBase, and it contains a list
of User objects, which I want to Serialize out to an XML file.

Is there anywhere where I can find how to decode it so that it recognizes
what objects are held in my list? or an example in C# prefereably?

Many thanks in advance...
Colin

Nov 12 '05 #1
2 10748
Colin,

You can use the XmlSerializer class. Take note of this (from the help file):

"The XmlSerializer gives special treatment to classes that implement
IEnumerable or ICollection. A class that implements IEnumerable must
implement a public Add method that takes a single parameter. The Add
method's parameter must be of the same type as is returned from the Current
property on the value returned from GetEnumerator, or one of that type's
bases. A class that implements ICollection (such as CollectionBase) in
addition to IEnumerable must have a public Item indexed property (indexer in
C#) that takes an integer, and it must have a public Count property of type
integer. The parameter to the Add method must be the same type as is
returned from the Item property, or one of that type's bases. For classes
implementing ICollection, values to be serialized will be retrieved from the
indexed Item property, not by calling GetEnumerator."

If you follow the above it will know what Type your collection contains.

Here is an example:

using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

// ...
string path = Application.StartupPath + @"\\Serialization.xml";

// Write the output to disk
StreamWriter sr = new StreamWriter(path, false, System.Text.Encoding.UTF8);
XmlTextWriter writer = new XmlTextWriter(sr);
XmlSerializer serializer = new XmlSerializer(typeof(MyUserCollection));

MyUserCollection users = new MyUserCollection();
users.Add(new User("Suzie"));
users.Add(new User("Jacob"));

serializer.Serialize(sr, users);
sr.Close();

// Deserialize
StreamReader xmltext = new StreamReader(path, true);
MyUserCollection usersDeserialized =
(MyUserCollection)serializer.Deserialize(xmltext);
--
Ross Donald
Rad Software
Free Regular Expression Designer @
http://www.radsoftware.com.au/web/Products/
"Colin Basterfield" <co**************@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
| Hi,
|
| I have a list which is derived from CollectionBase, and it contains a list
| of User objects, which I want to Serialize out to an XML file.
|
| Is there anywhere where I can find how to decode it so that it recognizes
| what objects are held in my list? or an example in C# prefereably?
|
| Many thanks in advance...
| Colin
|
|
|
Nov 12 '05 #2
Colin,

You can use the XmlSerializer class. Take note of this (from the help file):

"The XmlSerializer gives special treatment to classes that implement
IEnumerable or ICollection. A class that implements IEnumerable must
implement a public Add method that takes a single parameter. The Add
method's parameter must be of the same type as is returned from the Current
property on the value returned from GetEnumerator, or one of that type's
bases. A class that implements ICollection (such as CollectionBase) in
addition to IEnumerable must have a public Item indexed property (indexer in
C#) that takes an integer, and it must have a public Count property of type
integer. The parameter to the Add method must be the same type as is
returned from the Item property, or one of that type's bases. For classes
implementing ICollection, values to be serialized will be retrieved from the
indexed Item property, not by calling GetEnumerator."

If you follow the above it will know what Type your collection contains.

Here is an example:

using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

// ...
string path = Application.StartupPath + @"\\Serialization.xml";

// Write the output to disk
StreamWriter sr = new StreamWriter(path, false, System.Text.Encoding.UTF8);
XmlTextWriter writer = new XmlTextWriter(sr);
XmlSerializer serializer = new XmlSerializer(typeof(MyUserCollection));

MyUserCollection users = new MyUserCollection();
users.Add(new User("Suzie"));
users.Add(new User("Jacob"));

serializer.Serialize(sr, users);
sr.Close();

// Deserialize
StreamReader xmltext = new StreamReader(path, true);
MyUserCollection usersDeserialized =
(MyUserCollection)serializer.Deserialize(xmltext);
--
Ross Donald
Rad Software
Free Regular Expression Designer @
http://www.radsoftware.com.au/web/Products/
"Colin Basterfield" <co**************@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
| Hi,
|
| I have a list which is derived from CollectionBase, and it contains a list
| of User objects, which I want to Serialize out to an XML file.
|
| Is there anywhere where I can find how to decode it so that it recognizes
| what objects are held in my list? or an example in C# prefereably?
|
| Many thanks in advance...
| Colin
|
|
|
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection...
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
0
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
1
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
0
by: John Manion via .NET 247 | last post by:
Long Post, thanks for your patience... I have and XML file that looks something like this: <?xml version="1.0" encoding="utf-8" ?> <Settings> <Location> <X>30</X> <Y>40</Y> </Location>...
2
by: Samuel R. Neff | last post by:
What's the advantage of inheriting from CollectionBase as opposed to just implementing IList? It seems that it saves you from having to implement a few properties (Clear, CopyTo, Count,...
0
by: Romain TAILLANDIER | last post by:
Hi group I am quite new to xml. I have a strong typed collection derived from a collection base, and i need to serialize it. I have seriously search on the net and don't find any help about how...
1
by: davebaranas | last post by:
I am able to serialize this but I get a null exception when I try to deserialize it back Even if I don't make any child classes it throws "Object reference not set to an instance of an object."...
5
by: Tony | last post by:
Hello! Here I have a collection class Cards which is derived from the Base class CollectionBase. This class Cards is a container for Card object. Now to my question at the bottom of this class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.