对象XML序列化的问题? [英] Object xml deserialization issue?

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

问题描述

我的对象具有父子关系。每个子对象都有一个属性指向其容器。当这个对象在该应用创建的,它的设置,因此没有问题。这个父属性标有XmlIgnore属性,因为它需要被设置到它的运行时父实例。那么,什么是初始化此父属性的对象被反序列化后的最好方法是什么?是否有一个完成反序列化'事件或类似的东西?

My objects has a parent-child relationship. Each child object has a Parent property pointing to its container. When this object is created in the app, it's set, and thus no problem. This Parent property is marked with XmlIgnore attribute, because it needs to be set to its run-time parent instance. So, what's the best way to initialize this Parent property after the object is deserialized? Is there a 'Deserialize completed' event or something similar?

编辑:我在C#中的WPF谈论XmlSerializer的。我不想二进制序列。

I'm talking about XmlSerializer in C# WPF. I don't want binary serializer.

推荐答案

你的问题是有点详细欠缺,但是从一些属性和您所描述的属性,我将假设你使用的是的XMLSerializer 在.NET Framework。

Your question is somewhat lacking in details, but from some of the attributes and properties that you describe, I'm going to assume that you're using the XMLSerializer in the .NET Framework.

您可能知道关于<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx"相对=nofollow> OnDeserialized 属性,你可以用它来标记你想被称为一个对象已经反序列化后的特定方法。不幸的是,这仅适用于二进制,SOAP和数据属性格式化,没有的的的XMLSerializer

You may know about the OnDeserialized attribute, which you can use to mark a particular method that you want to be called after an object has been deserialized. Unfortunately, this only works with the Binary, SOAP, and DataAttribute formatters, not for XMLSerializer.

为了使用来完成相同的功能的的XMLSerializer ,你将不得不实施的 的IXmlSerializable 自己的类要序列化XML接口。这将允许你完成了控制你的类实例的序列化和反序列化,包括运行初始化属性的对象进行反序列化之后,code。

In order to achieve this same functionality using the XMLSerializer, you will have to implement the IXmlSerializable interface yourself on the class that you want to serialize to XML. This will allow you to complete control over how instances of your class are serialized and deserialized, including code that is run to initialize the Parent property after an object is deserialized.

目前在$ C $的CProject一个很好的例子,一篇文章,描述了如何正确地贯彻的IXmlSerializable 提供的这里

There is a good example article on CodeProject that describes how to correctly implement IXmlSerializable available here.

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

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