Different Serialization Technique In .NET

D

dotnet

Today, We are talking about serialization in .net. The serialization is a process where an object converted into a stream form that can be send with network and can be persist on storage location. The serialized thing may be file, object , text. The serialized form maintain the state of that format.To be convert same serialization format to same object is called deserialization.

The .net support some kind of serialization technique. Which are following...
Binary Serialization : Light and compact used in Remoting
SOAP Serialization : interoperable use SOAP and used in web Services
XML Serialization : Custom Serialization

First, XmlSerialization is converted object to xml format and all public members will be serialized. We can use attributes on serializable field like XmlElement,XmlAttribute,XmlIgnore etc. Xml serialization is benificial to send data between different platform. Like if we wanna send data across .netto java platform then it is a useful technique.

Second, Binaryserialization is a technique which serialized object to stream format. This serialized data to exact binary storage format and reconstruct the data from it. Besides, the other advantage of Binary Serialization is enhanced performance as This is faster and even more powerful in the sense that it provides support for complex objects, read only properties and even circular references. But It cann't be portable to another platform. The serializable object must be used [Serializable] attribute.

Third, SoapSerialization which is basically used as Soap protocol for communicating between application and service that support SOAP. The advantage of SOAP format is portability where data can be serialized with Soap and at the other side deserialized also with Soap format. The serializable object must be used [Serializable] attribute. The Soap Serializer does not supportserializing Generic Types of object.

The Custom Serialization technique is also used to serialized and deserialized data. This technique is used Binary or Soap Formatter for serialization.. We have to implement ISerializable interface on implemented class and define constructor and implement GetObjectData method. The constructor is usedto deserialize and GetObjectData method is used to serialized the data in specified format. The constructor and method are used SerializationInfo andStreamingContext parameters. The serializatinInfo stores the data object which are being serialized or deserialized and the StreamingContext define the storage location and caller context.

The example of all serialization is given below. For all Serialization We are using generic extension methods. These methods are being used for xml,binary and soap serialization each method takes a filepath argument and serialize or deserialize the object.


protected void Page_Load(object sender, EventArgs e)
{
string xmlFile = AppDomain.CurrentDomain.BaseDirectory + @"SerializeFiles\StudentXML.xml";
string soapFile = AppDomain.CurrentDomain.BaseDirectory + @"SerializeFiles\StudentSOAP.xml";
string dataFile = AppDomain.CurrentDomain.BaseDirectory + @"SerializeFiles\StudentDATA.text";
string customFile = AppDomain.CurrentDomain.BaseDirectory + @"SerializeFiles\StudentCustom.xml";

List list = new List { new Student { ID = 123, Name = "abc" }, new Student { ID = 254, Name = "xyz" }, new Student { ID = 5874, Name = "mnop" } };

//Xml Serialization
list.XmlSerialize(xmlFile);
var student = list.XmlDeSerialize(xmlFile);

//Binary Serialization
list.BinarySerialize(dataFile);
var bin = list.BinaryDeSerialize(dataFile);

//Soap Serialization
list.LastOrDefault().SoapSerialize(soapFile);
var soap = list.LastOrDefault().SoapDeSerialize(soapFile);

//Custom Serialization
StudentSerializer<list> studs = new StudentSerializer<list>(new List{new Student { ID = 32424, Name = "jkl" },new Student { ID= 9585, Name = "trew" }});
BinaryFormatter soapf = new BinaryFormatter();
using (FileStream stream = new FileStream(customFile, FileMode.Create))
{
soapf.Serialize(stream, studs);
}

using (FileStream stream = new FileStream(customFile, FileMode.Open))
{
var a = soapf.Deserialize(stream);
}

}

First we created files name for all serialization and then declared a generic list object that has to be serialized in different format. Then we called all serialize and deserialize method one by one that is being extended our list class. In the last we created generic list object of custom serialization class and call serialize and deseralize method.


[DataContract]
[Serializable]
[XmlRoot]
public class Student
{
[DataMember]
[XmlElement]
public string Name { get; set; }

[DataMember]
[XmlElement]
public int ID { get; set; }
}

The serializable student class is given about which is having different types of attribute on class and datamembers. These attribute are necessary forserialization.


[Serializable]
public class StudentSerializer : ISerializable where T : class
{
T stud;

public StudentSerializer(T student)
{
this.stud = student;
}

protected StudentSerializer(SerializationInfo info, StreamingContext context)
{
stud = info.GetValue("s1", typeof(T)) as T;
}

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("s1", stud);
}
}

The above is custom serialize implementation. As we describe above that we are using constructor and getobjectdata method for deserialize and serialize data. The implementation of custom serialization is generic type so any single or list can be used it.


public static class MySerializer
{
public static void XmlSerialize(this T serializeObj, string filePath) where T : class
{
using (StreamWriter writter = new StreamWriter(filePath))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(writter, serializeObj);
}
}

public static T XmlDeSerialize(this T deSerializeObj, string filePath) where T : class
{
using (StreamReader reader = new StreamReader(filePath))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
return serializer.Deserialize(reader) as T;
}
}

public static void BinarySerialize(this T serializeObj, string filePath) where T : class
{
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
BinaryFormatter binary = new BinaryFormatter();
binary.Serialize(fs, serializeObj);
}
}

public static T BinaryDeSerialize(this T deSerializeObj, string filePath) where T : class
{
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
BinaryFormatter binary = new BinaryFormatter();
return binary.Deserialize(fs) as T;
}
}

public static void SoapSerialize(this T serializeObj, string filePath) where T : class
{
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
SoapFormatter soap = new SoapFormatter();
soap.Serialize(fs, serializeObj);
}
}

public static T SoapDeSerialize(this T deSerializeObj, string filePath) where T : class
{
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
SoapFormatter soap = new SoapFormatter();
return soap.Deserialize(fs) as T;
}
}
}

Hope this description and example will clear you the serialization technique for different format. Happy Coding..

Namespace is being used

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml;
using System.Xml.Serialization;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top