473,566 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10762
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.Seri alization;
using System.IO;

// ...
string path = Application.Sta rtupPath + @"\\Serializati on.xml";

// Write the output to disk
StreamWriter sr = new StreamWriter(pa th, false, System.Text.Enc oding.UTF8);
XmlTextWriter writer = new XmlTextWriter(s r);
XmlSerializer serializer = new XmlSerializer(t ypeof(MyUserCol lection));

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

serializer.Seri alize(sr, users);
sr.Close();

// Deserialize
StreamReader xmltext = new StreamReader(pa th, true);
MyUserCollectio n usersDeserializ ed =
(MyUserCollecti on)serializer.D eserialize(xmlt ext);
--
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******** ********@tk2msf tngp13.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.Seri alization;
using System.IO;

// ...
string path = Application.Sta rtupPath + @"\\Serializati on.xml";

// Write the output to disk
StreamWriter sr = new StreamWriter(pa th, false, System.Text.Enc oding.UTF8);
XmlTextWriter writer = new XmlTextWriter(s r);
XmlSerializer serializer = new XmlSerializer(t ypeof(MyUserCol lection));

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

serializer.Seri alize(sr, users);
sr.Close();

// Deserialize
StreamReader xmltext = new StreamReader(pa th, true);
MyUserCollectio n usersDeserializ ed =
(MyUserCollecti on)serializer.D eserialize(xmlt ext);
--
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******** ********@tk2msf tngp13.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
6531
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 class: Derives MarshalByValueComponent Implements ICollection, IList and ISerializable Explicitly implements the IList methods as private members,...
5
24683
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 s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
0
2162
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 and properties derived from this class but will not serialize or deserialize the properties in CollectionBase. Like InnerList, which is a read only...
1
7078
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 and properties derived from this class but will not serialize or deserialize the properties in CollectionBase. Like InnerList, which is a read only...
0
2059
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> <Size>
2
6953
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, GetEnumerator, and RemoveAt) but the way it implements all the other things you need to override seems overkill and counters the advantage of having an...
0
1347
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 to make a collection serialisable with a real name (with not the ArrayOf prefix) There is a workaround by encapsulate the collection as a...
1
3655
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." I must be missing something here Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click
5
4830
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 we have a method called Contains. It gived the same result to use InnerList and List in this method Contains. I can also just replace List with...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.