Net-informations.com
SiteMap  | About    

What is .Net serialization

.Net Serialization ?

serialization

Serialization can be defined as the process of converting the state of an object instance to a stream of data, so that it can be transported across the network or can be persisted in the storage location. The advantage of serialization is the ability to transmit data across the network in a cross-platform-compatible format, as well as saving it in a persistent or non-persistent state of an object to a storage medium so an exact copy can be recreated at a later stage. Deserialization is its reverse process, that is unpacking stream of bytes to their original form.

Any attempt to pass the object as a parameter or return it as a result will fail unless the object derives from MarshalByRefObject. This process of serializing an object is also called deflating or marshalling an object. If the object is marshalling or it is marked as Serializable, the object will automatically be serialized.

The following program will show how to Serialize an Object and later De-serialize it

C# Source Code

using System; using System.IO ; using System.Windows.Forms; using System.Runtime.Serialization; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //serializing the object private void button1_Click(object sender, EventArgs e) { serializeObject srObj = new serializeObject(); srObj.srString = ".Net serialization test !!"; srObj.srInt = 1000; IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); Stream fileStream = new FileStream("c:\\SerializeFile.bin", FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(fileStream, srObj); fileStream.Close(); MessageBox.Show("Object Serialized !!"); } //de-serializing the object private void button2_Click(object sender, EventArgs e) { IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); Stream serialStream = new FileStream("c:\\SerializeFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read); serializeObject srObj = (serializeObject)formatter.Deserialize(serialStream); serialStream.Close(); MessageBox.Show(srObj.srString + " " + srObj.srInt.ToString ()); } } //specimen class for serialization [Serializable] public class serializeObject { public string srString = null; public int srInt = 0; } }

VB.Net Source Code

Imports System.Runtime.Serialization Imports System.IO Public Class Form1 'serializing the object Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim srObj As New serializeObject() srObj.srString = ".Net serialization test !!" srObj.srInt = 1000 Dim formatter As IFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter() Dim fileStream As Stream = New FileStream("c:\SerializeFile.bin", FileMode.Create, FileAccess.Write, FileShare.None) formatter.Serialize(fileStream, srObj) fileStream.Close() MsgBox("Object Serialized !!") End Sub ' De-serializing the object Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim formatter As IFormatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter() Dim serialStream As Stream = New FileStream("c:\SerializeFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read) Dim srObj As serializeObject = DirectCast(formatter.Deserialize(serialStream), serializeObject) serialStream.Close() MsgBox(srObj.srString + " " + srObj.srInt.ToString()) End Sub End Class 'specimen class for serialization <Serializable()> _ Public Class serializeObject Public srString As String = Nothing Public srInt As Integer = 0 End Class

There are three types of serialization in .Ne : Binary Serialization, SOAP Serialization and XML Serialization.

Binary Serialization

Binary serialization is the process where you convert your .NET objects into byte stream. In binary serialization all the public, private, even those which are read only, members are serialized and converted into bytes.

SOAP Serialization

SOAP is a protocol based on XML, designed specifically to transport procedure calls using XML. Because a SOAP message is built using XML, the XmlSerializer can be used to serialize classes and generate encoded SOAP messages.

XML Serialization

XML Serialization is the process of serializing a .Net Object to the form of XML or from an XML to .Net Object.

You can study a detailed lesson on XML Serialization from the following link :

C# XML Serialization    VB.Net XML Serialization







net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.