| .NET DotNet Forum Index » General Discussion » Using XmlSerializer for derived classes... |
|
Page 1 of 1 |
|
| Author |
Message |
| nagar at (no spam) community.nospam... |
Posted: Thu Oct 29, 2009 5:45 am |
|
|
|
Guest
|
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 |
|
|
| Back to top |
|
|
|
| Family Tree Mike... |
Posted: Thu Oct 29, 2009 5:51 pm |
|
|
|
Guest
|
nagar at (no spam) community.nospam wrote:
Quote: 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();
}
}
}
--
Mike |
|
|
| Back to top |
|
|
|
| nagar at (no spam) community.nospam... |
Posted: Mon Nov 02, 2009 4:04 am |
|
|
|
Guest
|
It worked. Thanks.
Andrea
On Thu, 29 Oct 2009 19:51:59 -0400, Family Tree Mike
<FamilyTreeMike at (no spam) ThisOldHouse.com> wrote:
Quote: nagar at (no spam) community.nospam wrote:
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 to top |
|
|
|
|