XML序列化 - 当要使用的DataContractSerializer /二进制/ XMLSerialiser [英] XML Serialisation - When To Use DataContractSerializer / Binary / XMLSerialiser
问题描述
我一直在看这一段时间了。
I ve been looking at this for a while now
似乎二进制序列化是气馁,因为任何改变字段名称打破系列化=?不好
It seems that binary serialisation is discouraged as any change to field names breaks serialisation =? Not Good
XMLSerializer的是有问题的,因为你虽然你们的确有过的元素为属性或元素和它们的命名更多的控制,以提供一个无参数的构造函数和公共领域
XMLSerializer is problematic because you have to provide a no arg constructor and public fields although you do have more control over elements being attributes or elements and their naming
DataContractSerializer的是好的,但所有suclassses需要明确添加这是一种耻辱。
DataContractSerializer is good but all suclassses need to be explicitly added which is a shame
不过我碰到NetDataContractSerializer偶然发现这不有此限制。
However I stumbled across NetDataContractSerializer which does not have this limitation.
如果你的目标是C#序列化和XML的规模没有大的限制是NetDataContractSerializer始终走在这里??
If your goal is C# serialisation and no big constraints on size of xml is NetDataContractSerializer always the way to go here??
推荐答案
丹Rigsby对的 XmlSerializer的对DataContractSerializer的并也触动了NetDataContractSerializer。
Dan Rigsby has a really good comparative article on XmlSerializer vs. DataContractSerializer and also touches on the NetDataContractSerializer.
DataContractSerializer的:
- 它的速度快 - 比XmlSerializer的速度快10%左右
- 这是互操作性 - 与Java,Ruby的完美的作品 - 你的名字
- 使用明确的选入模式 - 你需要的标记什么被序列化
- 不需要任何构造
- 可序列化非公共成员和内部字段
- 不支持上属性XML节点
- it's fast - around 10% faster than XmlSerializer
- it's interoperable - works flawlessly with Java, Ruby - you name it
- uses explicit "opt-in" model - you need to mark what gets serialized
- doesn't require any constructor
- can serialize non-public members and internal fields
- doesn't support attributes on XML nodes
您告诉DCS明确的什么可序列化,但你没有太多的影响力超过如何,它的完成。
You tell the DCS explicitly what to serialize, but you don't have much influence over how it's done.
的XmlSerializer
- 连载只有公共字段和属性
- 连载一切,除了那些你排除(退出模型)
- 支持的属性和一切
- 这是互操作性 - 与Java,Ruby的完美的作品 - 你的名字
- 需要反序列化
参数的构造函数
- serializes only public fields and properties
- serializes everything except those you exclude (opt-out model)
- support attributes and everything
- it's interoperable - works flawlessly with Java, Ruby - you name it
- requires a parameterless constructor for deserialization
您告诉XmlSerializer的非常清楚如何和序列化的东西,但你不能序列化的一切 - 仅公开可见的属性。
You tell the XmlSerializer pretty clearly how and what to serialize, but you cannot serialize everything - only publicly visible properties.
该NetDataContractSerializer是一个有点古怪的 - 这不是可互操作的,它的工作原理只有在两端.NET - 它包括.NET类型信息到消息(使它更大)。你不能声明将它添加到WCF服务开箱即用
The NetDataContractSerializer is a bit of an oddity - it's not interoperable, it works only if both ends are .NET - it includes .NET type information into the message (making it bigger). You cannot add it declaratively to a WCF service "out of the box".
这是一个艰难的权衡 - 一如既往。绝对远离任何二进制格式远 - 这不是向后兼容,脆,势必会给你头痛 - 使用它做这些标准的途径之一。哪一个是最好的为您给定的情景真的很难说 - 你必须搞懂这个问题为自己....
It's a tough trade-off - as always. Definitely stay away from any binary formatter - that's not backwards compatible, brittle, and bound to give you headaches - use one of those standard ways of doing it. Which one is the "best" for your given scenario is really hard to tell - you'll have to figure that one out for yourself....
这篇关于XML序列化 - 当要使用的DataContractSerializer /二进制/ XMLSerialiser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!