Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

.net - DataContractSerializer vs XmlSerializer: Pros and Cons of each serializer

My desktop application serializes objects using XmlSerializer. I was suggested to leverage DataContractSerializer instead.
Under which scenarios should I use 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...