Serializing an ArrayList of objects

  • Thread starter Thread starter John
  • Start date Start date
J

John

I would like to serialize an ArrayList of objects. When I try to the
serialize method I get an error message:

An unhandled exception of type 'System.InvalidOperationException'
occurred in system.xml.dll

Additional information: There was an error generating the XML
document.

My object has the [Serializable] attribute for the class and the
[XmlElement(IsNullable=true)] for the properties that might be empty.

The serialization code is as follows:

ArrayList clients = new ArrayList();
clients = clientDB.getClients();
StringWriter xmlText = new StringWriter();
XmlSerializer serializer = new XmlSerializer(typeof(Client));
serializer.Serialize(xmlText, clients );

Any and all help would be greatly appreciated.
Thanks,
John
 
If you try to serialize an arraylist, you get generic element names. See
example 1 below.

On the other hand if you set the arraylist as a public field or property of
a type, and apply attributes to that property, then you have the possibility
to better control the serialization. You can annotate the ArrayList
property to specify the name of the array and the name of the array items.
See example 2 below.

also see this article:
http://www.extremetech.com/article2/0,3973,1164030,00.asp

Also, a side note: it seems like you don't need to assign
clients = new ArrayList();

then assign again
clients = clientDB.getClients();

.....the first arraylist is never used?

And finally, I believe if you use the BinaryFormatter you can just serialize
the arraylist directly.
http://www.dotnetforums.net/t69685.html


-Dino

==============================================================
// example 1

using System.IO;
using System.Xml.Serialization;

public class Alist2 {

static void Main(string[] args) {
try {
System.Collections.ArrayList list1 = new
System.Collections.ArrayList();

list1.Add("Foo");
list1.Add("Bar");
list1.Add(true);
list1.Add(7);
list1.Add(1297);

System.Collections.ArrayList list2 = new
System.Collections.ArrayList();
list2.Add("Sublist");
list2.Add(3.14159);
list1.Add(list2);

StringWriter xmlText = new StringWriter();
XmlSerializer s = new
XmlSerializer(typeof(System.Collections.ArrayList));

s.Serialize(System.Console.Out, list1); // serialize self to
Console.Out
}
catch (System.Exception e1) {
System.Console.WriteLine("\n\n**** Exception: " + e1);
}
}
}

==============================================================
example 2

using System.Xml.Serialization;
using System.IO;

namespace Ionic {
//Thing class added to the ArrayList
public class Thing {
[XmlElement("IdNumber")]
public string Id;
[XmlElement("Color")]
public string Hue;

//Without this it wouldn't serialize properly
public Thing() {}

public Thing(string idNumber, string color) {
Id = idNumber;
Hue = color;
}
}

//Collection class wrapping the ArrayList - which is exposed as a public
field
[XmlRoot("List.Of.Things")]
public class Things {
[XmlArray("Items")]
[XmlArrayItem("A.Thing",typeof(Thing))]
public System.Collections.ArrayList List = new
System.Collections.ArrayList();
}


//Serialization class
class Driver {
static void Main(string[] args) {
Thing t1 = new Thing("1234","Black");
Thing t2 = new Thing("4321","Blue");

Things list1 = new Things();
list1.List.Add(t1);
list1.List.Add(t2);
list1.List.Add(new Thing("8877","Red"));
list1.List.Add(new Thing("1442","Greenish-Purple"));

//Serialize
string path = "Things.xml";
StreamWriter w = File.CreateText(path);
XmlSerializer s = new XmlSerializer(typeof(Things));
s.Serialize(w,list1);
w.Close();

StreamReader r = File.OpenText(path);
Things list2 = (Things)s.Deserialize(r);
r.Close();
s.Serialize(System.Console.Out,list2);
System.Console.WriteLine("\n\nalldone\n");
}
}
}


John said:
I would like to serialize an ArrayList of objects. When I try to the
serialize method I get an error message:

An unhandled exception of type 'System.InvalidOperationException'
occurred in system.xml.dll

Additional information: There was an error generating the XML
document.

My object has the [Serializable] attribute for the class and the
[XmlElement(IsNullable=true)] for the properties that might be empty.

The serialization code is as follows:

ArrayList clients = new ArrayList();
clients = clientDB.getClients();
StringWriter xmlText = new StringWriter();
XmlSerializer serializer = new XmlSerializer(typeof(Client));
serializer.Serialize(xmlText, clients );

Any and all help would be greatly appreciated.
Thanks,
John
 
Thank you for the reply.

Wrapping the Arraylist in it's own class did the trick.

Thanks,
John

The arraylist is a member of the object I am wanting to
serialize. it is being populated with the objects with
the call clientDB.getClients()
-----Original Message-----
I would like to serialize an ArrayList of objects. When I try to the
serialize method I get an error message:

An unhandled exception of
type 'System.InvalidOperationException'
occurred in system.xml.dll

Additional information: There was an error generating the XML
document.

My object has the [Serializable] attribute for the class and the
[XmlElement(IsNullable=true)] for the properties that might be empty.

The serialization code is as follows:

ArrayList clients = new ArrayList();
clients = clientDB.getClients();
StringWriter xmlText = new StringWriter();
XmlSerializer serializer = new XmlSerializer(typeof (Client));
serializer.Serialize(xmlText, clients );

Any and all help would be greatly appreciated.
Thanks,
John
.
 
Back
Top