.net DataContractSerializer 与 XmlSerializer:每个序列化器的优缺点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2505778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:06:40  来源:igfitidea点击:

DataContractSerializer vs XmlSerializer: Pros and Cons of each serializer

.netserialization

提问by Vitali Climenco

My desktop application serializes objects using XmlSerializer. I was suggested to leverage DataContractSerializerinstead.
Under which scenarios should I use DataContractSerializer?

我的桌面应用程序使用XmlSerializer. 有人建议我DataContractSerializer改用杠杆。
我应该在哪些场景下使用DataContractSerializer

Many thanks

非常感谢

Comments.
1. The output XML file is stored locally. No other applications deserialize objects from that XML file.
2. My application runs with .NET Framework 3.5 SP1.

注释。
1. 输出的 XML 文件存储在本地。没有其他应用程序从该 XML 文件反序列化对象。
2. 我的应用程序使用 .NET Framework 3.5 SP1 运行。

回答by marc_s

Dan Rigsby has the ultimate post on this - go read it!

Dan Rigsby 有关于这个的终极帖子——去阅读吧!

XmlSerializer vs. DataContractSerializer (web archive)

XmlSerializer 与 DataContractSerializer(网络存档)

He says all there is to say, and in a very convincing way.

他把所有要说的都说了,而且非常有说服力。

In short:

简而言之:

XmlSerializer:

XmlSerializer:

  • has been around for a long time
  • is "opt-out"; everything public gets serialized, unless you tell it not to ([XmlIgnore])
  • 已经存在很长时间了
  • 是“选择退出”;所有 public 都被序列化,除非你告诉它不要 ([XmlIgnore])

DataContractSerializer is:

DataContractSerializer 是:

  • the new kid in town
  • optimized for speed (about 10% faster than XmlSerializer, typically)
  • "opt-in" - only stuff you specifically mark as [DataMember]will be serialized
  • but anything marked with [DataMember]will be serialized - whether it's publicor private
  • doesn't support XML attributes (for speed reasons)
  • 镇上的新来的孩子
  • 针对速度进行了优化(通常比 XmlSerializer 快 10%)
  • “选择加入” - 只有您特别标记的内容[DataMember]才会被序列化
  • 但任何标记[DataMember]将被序列化-无论是publicprivate
  • 不支持 XML 属性(出于速度原因)