What are the best practices to store and maintain serialized objects in C#? Any strategies or patterns that apply?

What I have come to believe so far is this:

  • Prefer Json over XML, both for space and for speed, but xml is easier to query/mine data via LINQ to XML for bigger data sets.
  • For every single property, map explicitly to a serialized name. In the future, when you need to rename a property, your serialized data won't break. Attributes help with this.
  • Store some kind of version info on the serialized object, in case you need to mass-migrate data in the future

Update: (that I found out the hard way)

  • Store all datetimes in a uniform way throughout the whole application and throughout all versions. Both concerning format and time zone.
  • What operations will be done on those serialised data? Only read/write or something more complex?
    – Euphoric
    Jul 24, 2012 at 9:15
  • It's more of a general question but in my case I need to store small settings objects that might or might not be updated. Jul 24, 2012 at 9:17
  • 2
    4. Follow the rules until it makes more sense to break the rules. Jul 24, 2012 at 23:11
  • 1
    What do you mean? What are "the rules" in this case? Jul 25, 2012 at 8:04

2 Answers 2


I've done quite a bit of serialization over the years. Here are some things we came up with:

  • Prefer human readable formats over binary formats. I'd generally prefer Xml to Json mainly because you can use Xslt to transform Xml when you need to upgrade the version.

  • Whatever you are serializing stuff as, the deserialization logic and data storage should allow for versioning of the serialized data. Version info should probably be outside the serialized data, makes it a bit easier to get at and choose the right deserialization strategy.

  • Your serialization should have unit tests running both ways. IE, you should have a test deserializing the data and confirming you get everything imported correctly. And you should have something serialize the data and confirm it comes out as the correct json and xml.

  • If the point of said serialization is cross-platform, test against the other platform. Probably not as much of a problem now as platforms have matured, but there were times when things did not agree on how json should be formatted.


Mentioned points are practical and good to keep in mind. I would like to mention different types of serializations that might be useful to consider depending on your application needs.

  • Binary Serialization - it uses binary encoding to produce compact serialization for uses such as storage or socket-based network streams.
  • XML serialization - it serializes the public fields and properties of an object, or the parameters and return values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document. More info is available in System.Xml.Serialization namespace .
  • SOAP Serialization - it is a different flavor of XML serialization, where it can be used to serialize objects into XML streams that conform to the SOAP specification. SOAP is a protocol based on XML, designed specifically to transport procedure calls using XML. As with regular XML serialization, attributes can be used to control the literal-style SOAP messages generated by an XML Web service.

More information form official source - MSDN C# programming

Also all this need to be practices within SDLC process (meaning having environment to test, version control, etc. )

  • Why would you want to store as binary or soap? Soap as far as I understand is overbloated so as to avoid data corruption, something that is a non-issue while storing in a known system. Jul 25, 2012 at 10:30
  • 1
    it is about speed, the fastest is binary serialization.
    – Yusubov
    Jul 25, 2012 at 11:36
  • The BinaryFormatter is also the least change tolerant option. We've had endless struggles with it and now wish we'd never used it. See: stackoverflow.com/a/703361/505697
    – Arjailer
    Jul 25, 2012 at 11:53
  • sure, everything has it's own issues, and depends where it is used. as it is mentioned, for cross-platform compatibility it is not an ideal option, this is where XML serialization takes the stage.
    – Yusubov
    Jul 25, 2012 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.