Using XmlSerializer for derived classes

  • Thread starter Thread starter nagar
  • Start date Start date
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
 
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();
}
}
}
 
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();
}
}
}
 
Back
Top