简体   繁体   English

XML序列化 - 何时使用DataContractSerializer / Binary / 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 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 XMLSerializer是有问题的,因为您必须提供无参数构造函数和公共字段,尽管您可以更多地控制元素属性或元素及其命名

DataContractSerializer is good but all suclassses need to be explicitly added which is a shame DataContractSerializer很好,但需要明确添加所有的分类,这是一种耻辱

However I stumbled across NetDataContractSerializer which does not have this limitation. 但是我偶然发现NetDataContractSerializer没有这个限制。

If your goal is C# serialisation and no big constraints on size of xml is NetDataContractSerializer always the way to go here?? 如果你的目标是C#序列化并且对xml的大小没有很大的限制,那么NetDataContractSerializer总是可以去这里吗?

Dan Rigsby has a really good comparative article on XmlSerializer vs. DataContractSerializer and also touches on the NetDataContractSerializer. Dan Rigsby在XmlSerializer与DataContractSerializer上有一篇非常好的比较文章,并且还涉及NetDataContractSerializer。

DataContractSerializer: DataContractSerializer的:

  • it's fast - around 10% faster than XmlSerializer 它速度快 - 比XmlSerializer快10%左右
  • it's interoperable - works flawlessly with Java, Ruby - you name it 它具有互操作性 - 与Java,Ruby完美配合 - 您可以这么说
  • 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 不支持XML节点上的属性

You tell the DCS explicitly what to serialize, but you don't have much influence over how it's done. 你告诉DCS明确序列化的东西 ,但是你没有超过它是如何做多大的影响力。

XmlSerializer XmlSerializer的

  • 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 它具有互操作性 - 与Java,Ruby完美配合 - 您可以这么说
  • requires a parameterless constructor for deserialization 需要一个无参数构造函数来进行反序列化

You tell the XmlSerializer pretty clearly how and what to serialize, but you cannot serialize everything - only publicly visible properties. 您可以非常清楚地告诉XmlSerializer如何以及序列化的内容,但是您无法序列化所有内容 - 只有公开可见的属性。

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). NetDataContractSerializer有点奇怪 - 它不可互操作,只有当两端都是.NET时才有效 - 它在消息中包含.NET类型信息(使其更大)。 You cannot add it declaratively to a WCF service "out of the box". 您无法以声明方式将其添加到“开箱即用”的WCF服务中。

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.... 对于你给定的场景,哪一个是“最好的”真的很难说 - 你必须为自己想出一个......

Marc Gravell在他的博客上展示了他使用DataContractSerializer,XmlSerializer和大多数其他.NET序列化器的基准测试结果 ,包括protobuf-net(他创建的.NET协议缓冲区的实现)。

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

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