When should I use XML Serialization vs. Binary Serialization in the .NET framework?

前端 未结 3 1552
Happy的楠姐
Happy的楠姐 2020-12-31 05:39

I\'m confused - when should I be using XML Serialization and when should I be using Binary Serialization in the .NET framework?

3条回答
  •  臣服心动
    2020-12-31 06:11

    Both of the existing answers focus on "cross platform", but that is an unrelated issue. The point they are making there is "don't use BinaryFormatter if you are doing cross-platform" - which I entirely support. However there are a range of binary serialization formats that are very much cross-platform - protobuf / ASN.1 being prime examples.

    So, let's look instead at what each has to offer;

    • Binary is typically smaller, typically faster to process (at both ends), and not easily human readable / editable
    • Text formats (xml / json) tend to be more verbose than binary (although often compresses well), but are pretty easy to work with by hand; but all that text processing an mapping tends to make them slower
      • xml is very common is web-services, and benefits from gooling support such as xsd, xslt and robust xml editors
      • json is the main player in browser-based comms (although it is also used in web-services) - tends to be less formal but still very effective

    Notice how interoperability is neither a strength nor weakness of either, as long as you choose an appropriate binary format!

    Here's an answer that compares the serialization time, deserialization and space metrics of most of the .NET serializers, for your reference.

提交回复
热议问题