![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| XmlSerializer problem - help? | .NET General | |||
| XmlSerializer FileNotFoundException | .NET General | |||