在复杂对象图上使用 IXmlSerialized 接口
如果在包含具有构成复杂对象的属性的复杂对象上使用自定义 XML 序列化 (IXmlSerialable
),而这些属性不使用自定义 IXmlSerialized
接口,如何在 IXmlSerialized.ReadXml(XmlReader reader)
方法中指定希望反序列化器对这些子属性使用普通反序列化?
注意:类似于这个问题
If using custom XML Serialization (IXmlSerialiable
), on a complex object that contains properties with constituent complex objects which do NOT use custom IXmlSerializable
interface, how do you specify, in the IXmlSerializable.ReadXml(XmlReader reader)
method, that you want the deserializer to use the ordinary deserialization on those child properties?
NOTE: similar to this question
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
IXmlSerialized
实现起来有点乏味,因为它几乎是一种全有或全无的方法,因为您无法为正常的 XML 序列化选择子类型。但是,如果我理解正确,您可以通过为未实现IXmlSerialized
的类型手动创建XmlSerializer
来实现您想要的效果。例如,如果我们从两个类开始,
Default
没有实现IXmlSerialized
,而Custom
则实现了它。然后,我们创建第三个类
Parent
,它具有前面每个实例的子级,并以调用ReadXml/WriteXml
方法的方式实现IXmlSerialized
为支持它的子级并为另一个子级创建默认的 XML 序列化器。为了使示例完整,我们提供一个序列化和反序列化
Parent
实例的示例程序:The
IXmlSerializable
is a bit tedious to implement since it's pretty much an all or nothing approach given that you cannot select child types for normal XML serialization. However, if I understood you correctly you can achieve what you want by manually creatingXmlSerializer
for the types that do not implementIXmlSerializable
.For example, if we start with two classes,
Default
that does not implementIXmlSerializable
andCustom
which does implement it.Then we create a third class
Parent
that has a child of each of the previous instances and implementsIXmlSerializable
in a way that callsReadXml/WriteXml
methods for the child that supports it and create default XML serializer for the other child.To make the example complete, a sample program that serializes and deserializes a
Parent
instance: