XML Or Binary Serialization – That is the Question

NET 2.0 and 3.0 offer two very different ways to serialize object data into something else, namely XML and bytes. This article will compare the two methods for you, as well as offer some words of wisdom. XML Serialization saves memory when the size of the data contained in the object being serialized is less than the size of the class definition plus the data contained in the object. This is because Binary Serialization includes the class definition within its serialized results and XML Serialization does not.

Why do I care that XML Serialization doesn’t save class information? Because the serialized XML Data representing the object can be deserialized (from XML back into an object) into any object that you so desire. Given, of course, that the class has a name, and Serialization Attributes exist in the class definition, and they correctly map to the XML Elements that were generated upon the initial XML serialization process.

Because Binary Serialization stores the class information as well as the object data, the deserialization process is type safe. Binary serialization guarantees that what you serialize into a stream of bytes is exactly what comes out of it at the time you so choose to deserialize back into an object. This ensures that the object is reconstructed correctly back into the class that you expect it to be. This is the class that you originally instantiated an object from, as if to give birth to a child. How sad that you then decided to take that child and serialize it into an array of bytes. And thank goodness for Binary Serialization, because you can re-construct your baby without concern, given you have a reference to the assembly in which your baby sleeps.

One caveat with serialization; watch out for dependencies when sending binary byte[] or XML from one service to another. At the destination service, when you want to deserialize the bytes or XML, you will need to have a reference to the assembly that the original class was built into. Watch out for these catch 22 dependencies, and keep things as close to object or byte array as possible inside the destination service, this way you will have no dependency issues.

By the way, dependency is bad, in case you haven’t heard it out on the digital grapevine yet.

The XML serialization method is great for quick and dirty mapping of serializable properties, or mapping to a property which is complex (a class). But be careful, if the class that you are deserializing into is not the original class, and it changes down the road, you could have some very “hard to pinpoint” bugs buzzing around your head and causing you headaches. Overall, the binary serialization method is usually more efficient, saving up to fifty percent in size when dealing with larger objects.

However, if you need to easily read the serialized data, just want to cut some corners in a most un-professional manner, and you are not concerned with bandwidth or performance,serial irani  XML Serialization is for you. It is simply easier to read XML being sent from point A to point B than it is to read a binary stream, but it is much heavier.

From a practical standpoint, XML is terrific to use for debugging, when you are down in the trenches and on a tight timeline. Use XML Serialization throughout your code so that you can easily log an object and see its contents without clicking through a maze of pluses and minuses.

Use either, or use both methods of serialization when you need them. Ultimately, it will depend on your level of comfort, in tandem with the requirements around response time for your application.

From this point forward, armed with the valuable information that you read here, you will comfortably and appropriately answer the serialization question when it next rears its ugly head.

This post will, some day, be followed up with another explaining XML Serialization attributes and their undeniable, usefulness in XML Serialization.

Epiphanies abound,

An epiphany is the sudden realization or comprehension of the (larger) essence or meaning of something.

Often insignificant by itself, an epiphany illuminates a deeper or numinous foundational frame of reference.



Leave a Reply

Your email address will not be published. Required fields are marked *