XML serializing a list of a base class

元气小坏坏 提交于 2019-12-23 09:17:35

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/6172330/xml-serializing-a-list-of-a-base-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!