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