DataContractSerializer 与 XmlSerializer: 每个序列化程序的优缺点

我的桌面应用程序使用 XmlSerializer序列化对象。
在哪些情况下我应该使用 DataContractSerializer

非常感谢

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

47721 次浏览

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

XmlSerializer vs. DataContractSerializer (web archive)

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

In short:

XmlSerializer:

  • has been around for a long time
  • is "opt-out"; everything public gets serialized, unless you tell it not to ([XmlIgnore])

DataContractSerializer is:

  • 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 public or private
  • doesn't support XML attributes (for speed reasons)