469,366 Members | 2,243 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

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 10601
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by David Sworder | last post: by
reply views Thread by John Manion via .NET 247 | last post: by
2 posts views Thread by Samuel R. Neff | last post: by
reply views Thread by Romain TAILLANDIER | last post: by
1 post views Thread by davebaranas | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.