![]() |
Platforms, Frameworks & Libraries »
.NET Framework »
General
Beginner
.NET / XML SerializationBy SarafianAn article that tries to clear up the concept of serialization |
C++/CLI, C# 1.0, C# 2.0, C# 3.0, VB 7.x, VB 8.0, VB 9.0, Windows, .NET CF, Mobile, .NET 1.0, .NET 1.1, .NET 2.0, .NET 3.0, .NET 3.5, ASP.NET, Visual Studio, WebForms, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Being a developer that takes a lot from the internet, i decided to contribute some of my own. I always had some vague concept of the .Net serialization and yesterday I understood exaxtly why. Internet could be a great resource but it can be also greatly misleading. Thiw article tries to clarify the concepts of .NET Serialization and XML Serialization
When i first looked up the term serialization, basically i redirected to sites that explaing how to turn an object to an xml represantation. Lets call that XML Serialization, a term that i do not actually like. But .NET Serialization its not that. Its the mechanism to transport data through remoting.
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 and public properties or fields and then create the corresponding xml node. Deserialization also works that way.
This is sample general class for producing xml string for an object and its reverse procedure
public class Serializer
{
public static string ObjectToString(object obj)
{
try
{
XmlSerializer serializer = new XmlSerializer(obj.GetType(), "");
StringWriter writer = new StringWriter();
serializer.Serialize(writer, obj, null);
return writer.ToString();
}
catch(Exception)
{
}
return null;
}
public static bool StringToObject(string data,out object obj)
{
obj = null;
try
{
XmlSerializer serializer = new XmlSerializer(obj.GetType(),"");
StringReader reader = new StringReader(data);
obj=serializer.Deserialize(reader);
return true;
}
catch(Exception)
{
}
return false;
}
}
One of the mostly noted feature of .NET was serialization. The feature is used to trasport data between remoting calls. Behind the scenes something like this happens for binary transport protocol
MemoryStream ms = new MemoryStream();
BinaryFormatter bFormatter = new BinaryFormatter();
Derialize
bFormatter.Serialize(ms, objectToSerialize);
Deserialize
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. .Net automated mechanism uses reflection to find whats needs to be serialized. Because this is not an xml serialization, class 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 the ISerializeble and a constructor with the below signature.
The void ISerializeble.GetObjectData (SerializationInfo info, StreamingContext ctxt) is called by the CLR to acquire the serialized data and the 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 transfered.
As you can see .NET Serialization is entirely different concept from XML Serialization. In my opinion XML Serialization should only be used to Store configuration files and options class in a registry key.
For more info about how to implement a custom serialization and for performance gain data look at Tim Haynes Fast Serialization article, based on which i finally clarified things in my mind.
Any terminology I used like .NET Serialization and XML Serialization, might be incorrect.
Version 1.
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 11 Oct 2007 Editor: |
Copyright 2007 by Sarafian Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |