简体   繁体   English

序列化时修改数据

[英]Modify data while serializing

I've got the hierarchial structure of files and folders inside of my application. 我的应用程序内部具有文件和文件夹的层次结构。 Application works with absolute paths, which are stored in FileNode.Items list of strings. 应用程序使用绝对路径,该绝对路径存储在字符串的FileNode.Items列表中。

When i've got to save my project, I serialize FileNode class in XML. 保存项目后,我将XML序列化FileNode类。 But, I need to convert absolute paths to relatives (if possible) and then serialize. 但是,我需要将绝对路径转换为相对路径(如果可能),然后进行序列化。

So, my question is: Is there any solution to do it on the fly (ie any flag near the property which does any action with it) or i need to manually convert paths before every serialization and after every deserializaion? 因此,我的问题是:是否有任何解决方案可以随时执行(即,在属性附近执行任何操作的任何标志),或者我需要在每次序列化之前和每次反序列化之后手动转换路径?

Thank's a lot for your answers 非常感谢你的回答

You could make a separate property on your FileNode class which returns relative paths, and add the [XmlIgnore] attribute to the original property to prevent it from being serialized. 您可以在FileNode类上创建一个单独的属性,该属性返回相对路径,然后将[XmlIgnore]属性添加到原始属性以防止其被序列化。

Alternatively, you could implement IXmlSerializable to control the serialization yourself. 或者,您可以实现IXmlSerializable来自己控制序列化。

No, there's no automatic way to do that. 不,没有自动的方法可以做到这一点。

You can implement ISerializable or get your XML serialized object and make your paths relative through XmlDocument 您可以实现ISerializable或获取XML序列化的对象,并通过XmlDocument使其路径相对

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM