Click here to Skip to main content
13,453,778 members (44,700 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 11 Oct 2007

.NET / XML Serialization

, 11 Oct 2007
Rate this:
Please Sign up or sign in to vote.
An article that tries to clear up the concept of serialization.


Being a developer that takes a lot from the internet, I decided to contribute some of my own. I always had some vague concepts regarding .NET serialization, and yesterday, I got it all clarified. Internet could be a great resource, but it can also be greatly misleading. This article tries to clarify the concepts of .NET Serialization and XML Serialization.


When I first looked up the term serialization, basically, I got redirected to sites that explained how to turn an object to an XML representation. Let's call that XML serialization, a term that I do not actually like. But .NET serialization is not that. It is the mechanism to transport data through Remoting.

XML Serialization

The majority of articles on the internet have it wrong. There is no need for the class to be marked as [Serializable] or implement ISerializable. XML serialization actually uses Reflection to find any public properties or fields, and then creates the corresponding XML node. Deserialization also works that way.

This is a sample general class for producing an XML string from an object, and its reverse procedure:

public class Serializer
    public static string ObjectToString(object obj)
            XmlSerializer serializer = 
                new XmlSerializer(obj.GetType(), "");
            StringWriter writer = new StringWriter();
            serializer.Serialize(writer, obj, null);
            return writer.ToString();
        return null;

    public static bool StringToObject(string data,out object obj)
        obj = null; 
            XmlSerializer serializer = 
                    new XmlSerializer(obj.GetType(),"");
            StringReader reader = new StringReader(data);
            return true;
        return false;

.NET Serialization

One of the most noted features of .NET is serialization. The feature is used to transport data between Remoting calls. Behind the scenes, something like this happens for the binary transport protocol:

MemoryStream ms = new MemoryStream(); 
BinaryFormatter bFormatter = new BinaryFormatter();


bFormatter.Serialize(ms, objectToSerialize); 


Object_Type obj = (Object_Type)bformatter.Deserialize(ms); 

The class to be serialized must be marked with the [Serializable] attribute, and optionally must implement the ISerializable interface. The .NET automated mechanism uses Reflection to find what needs to be serialized. Because this is not XML serialization, classes that implement IDictionary and other interfaces cannot be serialized.

In order for deserialization to work, extra info is wrapped with the actual binary data that describes what data is for what in a matter of way. This makes the data produced larger, but keeps the job done for every valid serializable class.

If we want to override this mechanism, the class must implement ISerializeble.

void ISerializeble.GetObjectData (SerializationInfo info, StreamingContext ctxt) is called by the CLR to acquire the serialized data, and public MyObject(SerializationInfo info, StreamingContext ctxt) is used to populate the objects data according to serialization data, usually received through a Remoting call.

In the above methods, because we know exactly what we are going to add to the serialization stream, we gain speed because Reflection is not used. And if our data coding is clever enough, we gain from the smaller size of data that needs to be transferred.

Points of Interest

As you can see, .NET serialization is an entirely different concept from XML serialization. In my opinion, XML serialization should only be used to store configuration files and option classes in a Registry key.

For more info about how to implement custom serialization and get performance gain, look at Tim Haynes Fast Serialization article, based on which I finally clarified things in my mind.

Any terminology I have used, like .NET serialization and XML serialization, might be incorrect.


Version 1.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Greece Greece
I live in Athens Greece and currently I am working with Business scale application with .NET latest technologies

I've been developing applications for personal and friends usage with C++ using majorly Borland's various IDEs since 1994.
In 2002 I began working for an R&D institute where I was introduced to C# which I worships ever since.

I love core application development and I would like to publish more articles here and on my blog, but there is not enough time to do so.

I usualy "waste" my spare time watching sitcoms, preferable SCI-FI.
I would like to play chess but I can't find any real world players to hang out with.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
puromtec123-Feb-09 8:21
memberpuromtec123-Feb-09 8:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180318.3 | Last Updated 12 Oct 2007
Article Copyright 2007 by aSarafian
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid