Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Using XmlSerializer for derived classes

Reply
 
Old 4 Weeks Ago   #1 (permalink)
nagar


 
 

Using XmlSerializer for derived classes

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

My System SpecsSystem Spec
Old 4 Weeks Ago   #2 (permalink)
Family Tree Mike


 
 

Re: Using XmlSerializer for derived classes

nagar@newsgroup 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
My System SpecsSystem Spec
Old 3 Weeks Ago   #3 (permalink)
nagar


 
 

Re: Using XmlSerializer for derived classes

It worked. Thanks.
Andrea

On Thu, 29 Oct 2009 19:51:59 -0400, Family Tree Mike
<FamilyTreeMike@newsgroup> wrote:
Quote:

>nagar@newsgroup 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();
> }
> }
>}
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
XmlSerializer problem - help? .NET General
XmlSerializer FileNotFoundException .NET General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46