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



0
5/13/2004 6:46:44 AM
dotnet.xml 7266 articles. 0 followers. Follow

1 Replies
471 Views

Similar Articles

[PageSpeed] 51

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" <colinbasterfield@hotmail.com> wrote in message
news:%23kGrPYLOEHA.3944@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
|
|
|


0
ross143 (6)
6/6/2004 4:40:23 AM
Reply:

Similar Artilces:

Sender Filtering List
Hi Everybody, I have list of senders populated in my organization, but now i can´t add any domain or address in this list!!!! My question: Sender Filtering list have limit of senders??? Thank You!!!!! (My english is very bad...) On Wed, 12 Apr 2006 12:58:02 -0700, Daniel Reck <daniel.reck@allensp.com.br> wrote: >Hi Everybody, > >I have list of senders populated in my organization, but now i can�t add any >domain or address in this list!!!! > >My question: Sender Filtering list have limit of senders??? > >Thank You!!!!! (My english is very bad...) &...

Adding a customer to multiple marketing lists
Is there a way to add a customer to multiple marketing lists at once? When I try this by going to Actions, Add to Marketing Lists, it only lets me select one row. You can add an account to one marketing list from the Actions Menu. You will need a plugin which will add an account to many marketing lists. SS "Tara C. Brown" wrote: > Is there a way to add a customer to multiple marketing lists at once? When I > try this by going to Actions, Add to Marketing Lists, it only lets me select > one row. What plugin is necessary to do this? "SS" wrote: > You...

Serial Number Problem
Hi Everyone, we run Dynamics on our server anyway we had HUGE problems and lost a lot of work.....anyway after fixing lots up i still have one MAJOR problem. Serial numbers for items in the system do exist if you look up inventory ~ Serials. There is a list of serial numbers in stock ready to use. But when we go to do an invoice there are no serial numbers, it asks you if you want to overide the quantities... Our problem was we lost our inventory file therefore we had to put an old file back on...although these serial numbers are there we can not use them which also means i cant put t...

Public folder position in folder list.
After I migrated my users from Exchange 5.5 to Exchange 2003, I noticed that their public folder show at the top of the Folder list view in Outlook 2002. Does anyonw know wher eI cna make the change to put it at the bottom of the list after Mailbox and Personal Folders? -Brendan Did you restart outlook? It should be listed alphabetically after a restart. If not, try creating a new profile. -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours http://www.poremsky.com - http://www.cdolive.com Outlook Tips: http://www.outlook-tips.net/ Expert Zone http://www....

What is a distribution list in outlook?
Can any one help me what is a distribution list in outlook thanks zack jenkin zack.jenkin@zacksnetwork.co.uk A preset collection of Outlook contact addresses combined into one = master address for simple sending. Have you even tried F1 for a = definition? --=81 Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. How to ask a question: http://support.microsoft.com/KB/555375 After furious head scratching, Zack Jenkin asked: | Can any one help me what is a distribution list in outlook |=20 | thanks | zack jenkin | zack.jenkin@zacksnetwork.co.uk Y...

How do I create a chart out of a list, instead of number values?
I'm trying to create a Pie Chart from a list of Questionnaire results. Each questionnaire's results will be recorded in a row. For example, one of the questions is "Gender?" and I have asked 5 people. Results: Male = 3, Female = 2 My Spreadsheet therefore looks like this: A 1| Gender 2| Male 3| Female 4| Male 5| Female 6| Male Instead of: A | B 1| Gender 2| Male | Female 3| 3 | 2 I can't get a graph for the first one, but can for the second. You might be thinking why I don't want to use the 2nd method...

Transpose stacked lists of values
I have a column with repeating labels, and a second column with values. Example: Column A Column B Label1 Value1 Label2 Value2 Label3 Value3 Label1 Value4 Label2 Value5 Label3 Value6 Label1 Value7 Label2 Value8 Label3 Value9 I need to transpose these so that the Labels in column A become column headers, and the corresponding values for each label appear in the rows below each column header. Example: Label1 Label2 Label3 Value1 Value2 Value3 Value4 Value5 Valu...

Duplicate Element Names in Serialized XML
Hi All, Hoping someone can solve a problem for me. I am modifying the schema for a rather complex web service written in VB.NET. The services uses a large number of serialized classes to output the overall XML. The problem I am having is that I require 2 XML elements of the same name to be serialized from the 1 class. Due to the way the web service is written only 1 of these would be ever be called at a time. However, it seems that I get an exception if the same element name is used twice in one class.. any ideas? For example: 'start class here <XMLElement ("elementName&q...

Some links to file use drive letter, others network path, it thinks it is a different file!
I have a sheet with a bunch of lookups to another workbook on our server. I noticed one column that would not update with a Calculate. It sees the link as //Server/path etc... Other links will update and are linked to P:/path... When the file is opened P:/ links resolve to just the file name. The //Server ones don't. The only way to update the //Server lookups is to go into each one and edit and Enter to refresh. I am confused. Please help. I went into data links and sure enough, the same file was listed twice, once with drive letter, once with network path. So from there I opened...

Unable to import a Quicken ".qif" file into my brokerage acct. folder
Help! I'm trying to import all of my brokerage account activity as a Quicken ".qif" file into my Brokerage Account setup in Money 2003 under "Investment Accounts." When I try to import the ".qif" file into Money 2003, it doesn't recognize my Brokerage Account folder. What's the deal? Furthermore, at the present time my brokerage company doesn't have the electronically downloadable "direct statement" option. Therefore, the only way to update my brokerage account under Money 2003's Investment Accounts is either by importing...

Compare two lists
I have 2 columns of ref numbers and I need to compare the 2 columns so that I can identify the items in column 1 that are not in col 2. What is the best way to tackle this problem. Bob Hi see: http://www.cpearson.com/excel/duplicat.htm#InOneNotOther -- Regards Frank Kabel Frankfurt, Germany Box 666 wrote: > I have 2 columns of ref numbers and I need to compare the 2 columns > so that I can identify the items in column 1 that are not in col 2. > > What is the best way to tackle this problem. > > BobH Thanks Frank just what I was looking for. Bob "Frank Kabel...

Serial Motor
How do you control a motor via serial port? What language should I use? Justin Case wrote: > How do you control a motor via serial port? What language should I > use? I don't know, but it certainly isn't possible with SQL Server ... http://lmgtfy.com/?q=How+do+you+control+a+motor+via+serial+port -- HTH, Bob Barrows Bob Barrows (reb01501@NOyahoo.SPAMcom) writes: > I don't know, but it certainly isn't possible with SQL Server ... What? You can't control a motor with an engine? -- Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se...

file format is not valid
I changed computers and copied some excel files from my old computer. When I go to open them on the new computer I get the message file name .xls:file format is not valid. I am using the same excel programs as on my old computer. Thanks whoever. It sounds like the files have become corrupted in the copy. Hopefully you have the old machine! If not, try opening with Excel 2003, it can do some recovery. Open Office is even better at recovery I hear. Bob Flanagan Macro Systems http://www.add-ins.com Productivity add-ins and downloadable books on VB macros for Excel "elaines" &l...

serial port communuication
Hi all, I m communicating to a printer device through serial port.It is failing to open the port,but when I disable and enable the com port in device manager,it works fine(i.e)it communicates with the printer and reads the configuration settings of the printer). Regards Hari are u leaving the port opened when ur program terminates ........ what is the frequency of u disabling/enabling port ....... after every run ...... some times works some times not ..... ????? "hari" <haricibi83@gmail.com> wrote in message news:1171455246.402743.203120@k78g2000cwa.googlegrou...

Lost Outlook files in crash. Where are the is PST file?
I need to get the pst file for the new install but when I look in MS. Office files, I can't find the pst. Where is it? It says Office 10, 11 ,12 . . . . Don Smith <dhsmith@bellsouth.net> wrote: > I need to get the pst file for the new install but when I look in MS. > Office files, I can't find the pst. Where is it? It says Office 10, > 11 ,12 . . . . Open Windows Explorer and enter the following in the Address bar: %UserProfile%\Local Settings\Application Data\Microsoft\Outlook and click Go. I'll bet you'll find it. -- Brian Tillman ...

Saving a file takes very long
Hi there! There is a WinXP-Notebook with Office2000-sr1 installed. Now it takes very long to save XL-files. Even if I start a new sheet it takes about 5seks. to save. The filesize is only 14kB. A file with some graphics and about 2.2MB takes more than 40seks. This happens while saving to our LAN and to the local harddrive. The notebook is a P4 with 2GHz and 256MB RAM. Only Outlook2000 is opened at the same time. The "Journal" is deactivated on OL2k. Any ideas? Thanks for your help. Axel Try cleaning your windows temp folder. (Although, that usually gets blame for slow open...

Write/Read of wchar in buffer/file
Hello All, I have a buffer (lets say x) pointed to by WCHAR *. I have to write this buffer x to a file which also contains Non-Unicode chars. Finally I have to read the wchars x from the file and write them to a buffer pointed to by WCHAR *. Can anyone please let me know what calls I can use to store the buffer to the file and read from the file and copy it to the buffer ? Thanks, Viraj a.viraj@mobiliti.com http://www.mobiliti.com 732-248-8300 Let me see if I understand... you want to write a Unicode string to a file that may or may not contain non-Unicode characters and then read the...

List based on a date
I am looking for a function or code to extract from a list , names of those persons whose anuual fee payment due date is within next 15 days.This output list be a report or in a separate sheet which always reflect current status of the above function. Example :sheet 1 r/no -- col a------------col b----------------------------col c 1 ---name----------membership date--------------annual fee 2. ----david----------09-feb-2003--------------------$60 3. ------mark----------08-jan-2001--------------------$50 4. ------rosy-----------*********--------------------*** 5. ------cary-----------05-...

Serialization of jagged Arrays Issue
Hi there, I m trying to return an object (of my own written class) from a web service that contains jagged Arrays as public variables. Asp.Net is showing me the its serialized version on the browser when i invoke the service during test. Code: public class returnType { [System.Xml.Serialization.XmlElementAttribute("chassisdata")] public _chassisdata[] chassisdata; [XmlArray(),XmlArrayItem("wheel", typeof(_wheel[]),IsNullable=false)] public _wheel[][] upsteps; [XmlArray(),XmlArrayItem("wheelsize", typeof(_wheelsize[]),IsNullable=false)] publi...

File compatability between Window & Mac ver. of Words
Can file from Windows' MS Word 07 be open in Mac's Word 04 and vice- versa? In article <1182074057.363786.208840@g37g2000prf.googlegroups.com>, B.Destiny@gmail.com wrote: > Can file from Windows' MS Word 07 be open in Mac's Word 04 and vice- > versa? Word04 can only open .doc files. The .docx and .docm files produced by Word07 can be converted to .doc using the Office Open XML Converter.app available at Mactopia: http://www.microsoft.com/mac/downloads.aspx The converter is in beta, and has some limitations, but I've been able to successfully conver...

Distribution list- CC
My boss wants me to create a distribution list that will automatically cc: certain members when he selects the distribution list. Is it possible to open the distribution list and select certain addresses to be cc: only? Or possibly create a cc: distribution list nested inside the main distribution list? lisa <anonymous@discussions.microsoft.com> wrote: > My boss wants me to create a distribution list that will > automatically cc: certain members when he selects the distribution > list. Your boss can't have what he wants. > Is it possible to open the distribution lis...

Mail List with external address
Hello, Is there any way i can setup an account to distribute mail to users outside my domain? for example: When i send mail to group@mydomain.com which contains user1@gmail.com, user2@yahoo.com, user3@microsoft.com,... all of them gets the e-mail and can reply back to group@mydomain.com to talk to all the other users that could be domain users or not. In linux i only had to add the mail in /etc/aliases. So if i want a group called developers i only need to put email address in the aliases for this group no matter if it was a domain user or not. In windows when i create a distrib...

Deserializing with XmlSerializer and XML namespaces
I'm using SQLXML to get XML documents from a SQL Server database and deserializing the results with an XmlSerialier. It was working perfectly until I started getting namespace prefixes in some of my elements (due to using import directives in the mapping schemas). Here's a relevant XML fragment-- the element wit the y0: namespace prefix does not deserialize. How can I make the deserializer correctly deserialize this element (which is nested within an XmlArray-attributed collection)? <UserList> <User ID="157" FirstName="Joe" LastName="Someone" Em...

Disable item in a list box
Hi All, how can i delete a single item in a list box? -- -Sarath -- -Sarath "Sarath" wrote: > Hi All, > how can i delete a single item in a list box? > -- > -Sarath Sorry I meant how to to disable an item in a list box I meant how to disable a single item in a list box. sorry for the typo -- -Sarath "Sarath" wrote: > Hi All, > how can i delete a single item in a list box? > -- > -Sarath Sarath wrote: > I meant how to disable a single item in a list box. > sorry for the typo The list box does not have this capability built in bu...

No text converter is installed for this file type
This is a multi-part message in MIME format. ------=_NextPart_000_0009_01CA6771.CB83FA70 Content-Type: text/plain; charset="big5" Content-Transfer-Encoding: quoted-printable Hi Expert=20 I am now facing a problem in opening a ppt in Powerpoint 2003, an error = display as follow: "PowerPoint can't read the outline from filename. No text converter is = installed for this file type." My platform status: 1. Windows XP Professional SP3 2. Microsoft Office 2003 SP3 3. Office 2007 Compatibility Pack installed Who can help me? TKNL __...