Serializing a list of different object types.

  • Thread starter Thread starter chaitanyag
  • Start date Start date
C

chaitanyag

Hi,

I have my data stored in a set of classes (or structs, doesn't matter),
which I am trying to serialize. These classes are stored in an
ArrayList, which serializes ok when all the objects in the list are of
the same type. For example, if i have

public class A
{
string A1;
string A2;
}

In my container class, I just have

public class Container
{
private System.Collections.ArrayList all_data;
...

[XmlElement("MyData", typeof(A))]
public System.Collections.ArrayList

...
}

This works fine.

Lets say I have another class that contains a different set of data,

public class B
{
string B1;
int B2;
struct B3;
}

Again, I can use

[XmlElement("MyData", typeof(B))]
public System.Collections.ArrayList

which will work fine as long as the list contains only type B classes.
Is there any way I can add both class A and class B to my list and have
it serialized?

Any help/pointers would be much appreciated.
 
I have my data stored in a set of classes (or structs, doesn't matter),
which I am trying to serialize. These classes are stored in an
ArrayList, which serializes ok when all the objects in the list are of
the same type. For example, if i have

Use
[XmlArrayItem("MyDataA", typeof(A))]
[XmlArrayItem("MyDataB", typeof(B))]
public System.Collections.ArrayList

Mark
 
Back
Top