XML Serialization

  • Thread starter Thread starter Allen Bradley
  • Start date Start date
A

Allen Bradley

How can i serialize an ArrayList of object with difference Type?

Example:

ArrayList list = new ArrayList();
list.Add(new Type1());
list.Add(new Type1());
list.Add(new Type2());
list.Add(new Type3());
list.Add(new Type3());
list.Add(new Type3());

Best Regards
 
I don't think there is any easy way to do that. You would need to convert
each type to an array and have a property for each type in which you would
filter during serialization. ie:

[XmlElement("Type1")]
public Type1[] Type1
{
get
{
//Here work out how many type1 objects you have - I've
hard-coded to
//2
Type1[] type1Obj = new Type1[2];
//Set each type1Obj to the original arrayList - would be
//better to have a dictionary instead of an arraylist.

return type1Obj;
}
set
{
Type1[] type1Obj = (Type1[])value;
foreach (Type1 type1 in type1Obj)
list.Add(type1);
}
}

Now do the same for Type2, Type3...etc

Not the most elegant solution but should work.
 
Back
Top