Using XmlSerializer for derived classes

N

nagar

I'm using XMLSerializer to serialize an object that contains a generic
list

List <ChildBase> Children {get;set}

The problem is that each element derives from `ChildBase` which in
fact is an abstract class.
When I try to deserialize, I get an invalidOperationException

Is there a way I can use XMLSerializer with derived objects?

Thanks.
Andrea
 
F

Family Tree Mike

I'm using XMLSerializer to serialize an object that contains a generic
list

List <ChildBase> Children {get;set}

The problem is that each element derives from `ChildBase` which in
fact is an abstract class.
When I try to deserialize, I get an invalidOperationException

Is there a way I can use XMLSerializer with derived objects?

Thanks.
Andrea

I think we need to see what you are doing. This case shows how it
should work:

namespace ConsoleApplication1
{
[XmlInclude(typeof(Car))]
[XmlInclude(typeof(Truck))]
public class Container
{
public List<ChildBase> Children { get; set; }
public Container() { Children = new List<ChildBase>(); }
}

public abstract class ChildBase {}

public class Car : ChildBase
{
public Car() { }
}
public class Truck : ChildBase
{
public Truck() { }
}

class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

Container c = new Container();
c.Children.Add(new Truck());
c.Children.Add(new Car());
c.Children.Add(new Truck());
XmlSerializer s = new XmlSerializer(typeof(Container));
s.Serialize(sw, c);

StringReader sr = new StringReader(sb.ToString());
Container d;
XmlSerializer t = new XmlSerializer(typeof(Container));
d = (Container) t.Deserialize(sr);

d.Children.ForEach(x => Console.WriteLine(x));

Console.WriteLine("Done");
Console.ReadKey();
}
}
}
 
N

nagar

It worked. Thanks.
Andrea

I'm using XMLSerializer to serialize an object that contains a generic
list

List <ChildBase> Children {get;set}

The problem is that each element derives from `ChildBase` which in
fact is an abstract class.
When I try to deserialize, I get an invalidOperationException

Is there a way I can use XMLSerializer with derived objects?

Thanks.
Andrea

I think we need to see what you are doing. This case shows how it
should work:

namespace ConsoleApplication1
{
[XmlInclude(typeof(Car))]
[XmlInclude(typeof(Truck))]
public class Container
{
public List<ChildBase> Children { get; set; }
public Container() { Children = new List<ChildBase>(); }
}

public abstract class ChildBase {}

public class Car : ChildBase
{
public Car() { }
}
public class Truck : ChildBase
{
public Truck() { }
}

class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

Container c = new Container();
c.Children.Add(new Truck());
c.Children.Add(new Car());
c.Children.Add(new Truck());
XmlSerializer s = new XmlSerializer(typeof(Container));
s.Serialize(sw, c);

StringReader sr = new StringReader(sb.ToString());
Container d;
XmlSerializer t = new XmlSerializer(typeof(Container));
d = (Container) t.Deserialize(sr);

d.Children.ForEach(x => Console.WriteLine(x));

Console.WriteLine("Done");
Console.ReadKey();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top