Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML serializing a list of a base class

I have class A inherits from class B. I have a list of class B that contains A and B items:

List<B> myList = new List<B>();
myList.Add(new A());

When I try to serialize this list using XmlSerializable, an exception is thrown. If I define the list to of type B, then I do not get this exception. What is the best way to serialize the derived class?

like image 297
Steve Avatar asked May 30 '11 04:05

Steve


1 Answers

XmlIncludeAttribute will do the trick.
If you go to this url, you will find a code sample there too.

Also, in the example the attribute is applied to the method, but you might want to apply it class A in your case.

like image 167
Alex Aza Avatar answered Oct 14 '22 22:10

Alex Aza