是否可以将 DataContractSerializer 设置为忽略文件中的错误而不是完全失败?
我使用 DataContractSerializer 将构成树结构的大量不同类保存到 XML 文件。我正处于编写该软件的初始阶段,因此此时所有不同的组件都发生了很大的变化。然而,每次我对类进行更改时,我最终都会破坏程序打开以前保存的文件的能力。
如果缺少组件,我的树结构仍然可以发挥作用。有没有什么方法可以告诉 DataContractSerializer 跳过反序列化时出现问题的数据并继续,而不是在遇到第一个问题时退出?
我知道一个答案是编写我自己的序列化类,但我不想花时间这样做。我希望仍然能够利用 DataContractSerializer,但不会出现全有或全无的情况。
I'm using DataContractSerializer to save a large number of different classes which make up a tree structures to XML files. I'm in the initial stages of writing this software so at this point all the different components are changing around quite a bit. Yet every time I make a change to a class I end up breaking my programs ability to open previously saved files.
My tree structures will still be functional if components are missing. Is there some way to tell DataContractSerializer to skip over data it has a problem deserializing and continue on rather then just quitting at the first problem it has?
I know one answer would be to write my own serialization class, but I'd rather not spend the time to do that. I was hopping to still be able to take advantage of DataContractSerializer, but with out it being an all or nothing situation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在寻找的是 IExtensibleDataObject。这样,任何意外的元素都会被读入内部维护的名称-值字典中,甚至可以稍后序列化回来。请参阅以下资源寻求帮助。
博客文章 -- WCF可扩展性 - 其他序列化扩展
向前兼容的数据协定
< a href="http://msdn.microsoft.com/en-us/library/ms731138.aspx" rel="nofollow">数据合同版本控制
I think what you're looking for is IExtensibleDataObject. This way, any unexpected elements get read into a name-value dictionary maintained internally, and can even be serialized back later. See the following resources for help.
Blog post -- WCF Extensibility – Other Serialization Extensions
Forward-Compatible Data Contracts
Data Contract Versioning