Click here to Skip to main content
15,883,883 members
Articles / Operating Systems / Windows
Article

Serialization in .NET

Rate me:
Please Sign up or sign in to vote.
1.39/5 (41 votes)
25 Oct 20042 min read 111.8K   37   3
Serialization in .NET

Introduction

Serialization is a process of taking an object and converting into a form so that it can be transported across the network or can be persisted in the storage location. This storage location can be physical file, database or ASP.NET Cache. The form contains the state of the object so that by this format, we can construct the same object a later point in time, which is called Deserialization. <o:p>

<o:p> 

There are three formats of serialization<o:p>

<o:p> 

Binary Serialization             :        Light and compact used in Remoting<o:p>

SOAP Serialization            :        interoperable use SOAP and  used in web Services<o:p>

XML Serialization               :        Custom Serialization<o:p>

<o:p> 

<o:p> 

<o:p> 

<o:p>XML Serialization

For XML serialization, you need to use the attributes and specify them for each and every public member that you need. But since it is limited that it can serialize only public members, Serization done by it is called custom serialization. It is also known as Shallow Serialization<o:p>

<o:p> 

<o:p>SOAP and Binary Serialization

XML serializes only public members of the class. You use SOAP or Binary serialization when you need to transport data across the network. SOAP sends it using HTTP Protocol which makes it most interoperable while Binary serialization is known for its light and compact nature. Web Services uses the SOAP Serialization and Remoting uses the Binary Serialization. Infact Serialization is always neccessary when you need the object to transfer across a network. Advantage of using the SOAP or Binary serialization is that you can serialize the entire object and all those object that are being refrenced by it. This is why it is also called Deep Serialization.  If you want any class to serialize through any of these methods then you should use [Serializable] attribute on that class and then you can use the SoapFormater class or BinaryFormatter class to do the serialization. These classes have Serialize and DeSerialize method.  If you will not use SerializableAttribute for the class, then it will raise the exception. <o:p>

<o:p> 

Though this is the easiest way but at time you need the way so that you can decide what fields to serialize and how the serialization actually occurs. You can implement the ISerializable interface in the class. You need two things for that<o:p>

<o:p> 

  1. Constructor that is overridden and can handle the Deserialization process<o:p>
  2. GetObject method that tracks about which data is serialized.

A small example below illustrate this all.

 

<o:p> public class Employee :ISerializable

<o:p>{

<o:p>private int emp_no;

<o:p>private string name;

<o:p> 

<o:p>protected TestData(SerializationInfo info,StreamingContext context)
    {
      this.emp_no = info.GetInt32("emp_no");
      this.name = info.GetString("name");
    }

<o:p> void ISerializable.GetObjectData(SerializationInfo info,   

<o:p>         StreamingContext context)
      {
      info.AddValue("emp_no", this.emp_no);
      info.AddValue("name", this.name);
    }

}

<o:p>}

<o:p> 

<o:p> 

<o:p>I hope this introductory serialization information can help you to understands about Serialization with its need  and its effective usage.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Nishith Pathak is an avid reader,Prolific writer, Speaker for Microsoft India,Published Author of APress and a Microsoft Purist.He is MVP, MCPD, MCTS MCSD.Net,MCAD.Net(EA),MCSD. His proficiency lies in exploring Microsoft technology and to share them with other peers. He is a contributing author and an avid technical reviewer for multiple electronic and print publications. He has recently co-authored a book on WCF called Pro WCF: Microsoft Practical SOA Implementation for APress Inc, USA. Over the years, he has also been involved in providing consultancy and training services to corporations. He has been awarded with Microsoft Most Valuable Professional (MVP).Working since beta version of .Net makes his competecy in .Net. He can be contacted at NisPathak@Hotmail.com or at his blog http://DotNetPathak.Blogspot.com. Currently he is focused on key areas of the Microsoft platform, specifically Distributed Computing, service orientation and exploring VISTA and help companies architecting solutions based on Service Oriented Architecture.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Pranit M2-Feb-13 22:46
Pranit M2-Feb-13 22:46 
GeneralMy vote of 3 Pin
amrubinnaz22-Sep-11 19:48
amrubinnaz22-Sep-11 19:48 
GeneralMy vote of 5 Pin
Sunasara Imdadhusen17-Aug-11 17:14
professionalSunasara Imdadhusen17-Aug-11 17:14 

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.