473,396 Members | 1,891 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.

Serializing an ArrayList into XML File

Hello all,

I'm trying to persist an object of type Arraylist into XML using the
following the XMLSerializer and it returns an "Unknown" Error. I'm able to
serialize and deserialize the arraylist object using Binary format but
unable to do so using XML formatter. Is it possible to serialize an
ArrayList object into XML? If somebody could pass on some sample code that
would be really great.

Thanks,
Chris
Nov 11 '05 #1
1 12604
I'm new to XML in .NET, but here goes.

The XmlSerializer doesn't know what to do with the ArrayList elements.
You can convert the the ArrayList to an array of objects and annotate that
array with attributes so that the XmlSerializer will know what to do.

There may be a more conise way of doing this, but I don't know what it is;

using System;

using System.Collections;
using System.Xml.Serialization;

namespace arraylist
{
public class DriversLicense
{
public string state;
public string number;

public DriversLicense() {
this.state = null;
this.number = null;
}
public DriversLicense( string state, string number ) {
this.state = state;
this.number = number;
}
}

public class SocialSecurityNumber
{
public string number;

public SocialSecurityNumber() {
number = null;
}
public SocialSecurityNumber( string number ) {
this.number = number;
}
}

public class Identification
{
[XmlElementAttribute("SocialSecurityNumber",
typeof(SocialSecurityNumber))]
[XmlElementAttribute("DriversLicense", typeof(DriversLicense))]
public object[] Items;
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
ArrayList al = new ArrayList();
al.Add( new SocialSecurityNumber( "333-22-4444" ) );
al.Add( new DriversLicense( "NY", "1234567890" ) );

Identification ident = new Identification();
ident.Items = al.ToArray();

XmlSerializer ser = new XmlSerializer( typeof(Identification) );
ser.Serialize( Console.Out, ident );

Console.ReadLine();
}
}
}

"Christopher Pragash" <ch**********@hotmail.com> wrote in message
news:OD**************@tk2msftngp13.phx.gbl...
Hello all,

I'm trying to persist an object of type Arraylist into XML using the
following the XMLSerializer and it returns an "Unknown" Error. I'm able to
serialize and deserialize the arraylist object using Binary format but
unable to do so using XML formatter. Is it possible to serialize an
ArrayList object into XML? If somebody could pass on some sample code that
would be really great.

Thanks,
Chris

Nov 11 '05 #2

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

Similar topics

1
by: Pure Krome | last post by:
How do i serialize two instances (of some object) in the same class? I'm not sure how i would de-serialize a class that has two or more instances of some object type, and how the deserialize...
4
by: Christopher Pragash | last post by:
Hello all, Following is the code that I'm using to serialize an arraylist. This code returns an "Unknown" Errror. Any ideas would be of great help. <System.Xml.Serialization.XmlArray(), _...
0
by: Tom Kent | last post by:
I have an array list of objects that I want to serialize into an XML file. I have tried this using the following code, but it gives me an error message An unhandled exception of type...
3
by: Daniel Lidström | last post by:
Hello, I want to have a class that contains only a collection of another class. For example: public __gc class Alignment { public: Alignment(); ... };
1
by: dotNetDave | last post by:
I'm trying to create xml seriaizable collection class (below), but the xml keeps coming out wrong. In the resulting xml from the web service (below) the "ArrayOfAlarmProcessor" tag should really be...
0
by: Ante Smolcic | last post by:
Hi all, I have an ArrayList that contains items of type A. I declared the XmlArrayItem atribute for that type. Now I have an derived type B (from A) also contained in the ArrayList but I get...
1
by: Richard Atkinson | last post by:
I would appreciate help on the Microsoft Message Queue. Basically I would like to Serialize an entire class into a message queue and then reinstantiate the class in another application from the...
0
by: Yofnik | last post by:
Is there a way to avoid serializing an empty ArrayList? I would like the following class: public class Collection { public ArrayList Items = new ArrayList(); } ....to serialize like this:
0
by: Nodir Gulyamov | last post by:
Hello All, First of all thanks in advance to everyone who will try to help me. I have Form with TreeView control and DataGridView control. Each TreeNode has its own rows and due to I am placing...
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:
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
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...
0
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,...

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.