XML序列化的基类的列表 [英] XML serializing a list of a base class

查看:160
本文介绍了XML序列化的基类的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类 A 从类继承 B 。我有类 B 包含列表 A B 项目:

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());

当我尝试使用序列化 XmlSerializable 这个名单,将引发异常。如果我定义列表以B型的,那么我没有得到这个例外。什么是序列化派生类的最佳方法是什么?

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?

推荐答案

XmlIncludeAttribute 会做的伎俩。
如果你去这个网址,你会发现一个code样品有太多。

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

此外,在本例中该属性应用于方法,但你可能想要应用A级你的情况。

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

这篇关于XML序列化的基类的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆