Serialization & Deserialization in C#


Serialization and Deserialization in C# step by step

Serialization & Deserialization in C#

Hello Friends, Today our tutorial is on Serialization & Deserialization In C#.Many times my followers ask me that give us the solution on Serialization & Deserialization with XML/List In C#.

So,Let’s start our ttutorial without wasting our time….

The concept of Serialization and deserialization is used whenever data pertaining to objects have to be sent from one application to another. Serialization is used to export application data into a file. 

The destination application then uses deserialization to extract the data from the application for further use.

What is Serialization?
C# Serializable | C# Serialization - By Microsoft Award MVP - c# ...
Serialization is the process of using an object into a form that will be use din our projects. It's the process of converting the object into a form so that it can be stored on a file, database, or memory; or, it can be transferred across the network. Its main purpose is to save the state of the object so that it can be recreated when needed.

What is Deserialization?
C# Deserialization - javatpoint

Deserialization is the reversaltechnic of serialization. It is the process of getting back the serialized object so that it can be loaded into memory. It resurrects the state of the object by setting properties, fields etc.

Types :-
·         Binary Serialization
·         XML Serialization
·         JSON Serialization

Serialization is a type of concept in which C# class objects are written or serialized to files.Serializing can be used to directly write the data properties of the class to a file and Deserialization is used to read the data from the file and construct the object again.

Let's use a small example  on it:-

Program :-
XML Serialization for C++ Objects - CodeProject

using System;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Seriazation
{
[Serializable]
class Seriazation
{
public int ID;
public String Name;
static void Main(string[] args)
{
s val= new s();
val.ID = 1;
val.Name = "hello";

IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(@"home/new_file.txt",FileMode.Create,FileAccess.Write);

formatter.Serialize(stream, obj);
stream.Close();

stream = new FileStream(@"home/new_file.txt",FileMode.Open,FileAccess.Read);
s obj = (Tutorial)formatter.Deserialize(stream);

Console.WriteLine(obj.ID);
Console.WriteLine(obj.Name);

Console.ReadKey();
}
}
}

Code Explanation:-

  1. 1. We create the object "stream" to open the file Serialization.txt in reading only mode.
  2. 2. We then use the formatter class which is used to deserialize the object, which is stored in the Serialization.txt  file. The object returned is set to the object obj.
  3. 3. Finally, we display the properties of the object "obj" to the console using the "ID" and "name" properties.


When the above code is set, and the project is run using Visual Studio, you will get the below output.
Output:-

You can see from the above output that the values from the file were deserialized properly and displayed in the console.

Summary

Serialization is used to write class objects to files.
De-Serialization is used to recover the objects from the file.

Serialize object in XML format :-

C# Serialization & Deserialization with Example

For xml serialization use XmlSerializer instead of BinaryFormatter.

XmlSerializer xs = new XmlSerializer(typeof(Employee));  

Change the filename from "serialization.binary" to "serialization.xml".

and use [Xmlgnore] instead of [NonSerialized] in Serialization class. The rest will be the same.

Add a click event for Serialize button. When clicking on the serialize button we want to serialize the object of class S and store it in a file named "serialization.binary".

private void Serialization(object sender, EventArgs e) 
{ 
Ss = new S { 
name = textBoxName.Text, 
phone = textBoxPhone.Text, 
dob = dateTimePickerDoB.Value, 
department = textBoxDepartment.Text, 
salary = Convert.ToInt32(textBoxSalary.Text), 
additionalInfo = "We don't want it to serialize"
}; 

BinaryFormatter val= new BinaryFormatter(); 

FileStream a= new FileStream("serialization.binary", FileMode.Create, FileAccess.Write, FileShare.None); 
try 
{ 
using (a) 
{ 
val.Serialize(a, s); 
label6.Text = "Object Serialized"; 
} 
} 
catch 
{ 
label6.Text = "An error has occured"; 
} 
} 

Use BinaryFormatter to serialize the object in BinaryFormat. Make a file using FileStream named "serialization.binary". In this file your serialized object will be stored.value.Serialize(fsout, s) will serialize the object "s" and store it in file "serialization.binary".

The next thing is to write the code to deserialize the object. Add a click event for Deserialize button. When clicking on the Deserialize button we want to deserialize the object and show its values on screen.

 C# Serialization & Deserialization with Example

private void Deserialization(object sender, EventArgs e) 
{ 
Ss = new S(); 

BinaryFormatter val= new BinaryFormatter(); 

FileStream val1 = new FileStream("serialization.binary", FileMode.Open, FileAccess.Read, FileShare.None); 
try 
{ 
using (val1) 
{ 
s = (S) val.Deserialize(val1); 
label6.Text = "Object Deserialized"; 

textBoxName.Text = s.name; 
textBoxPhone.Text = s.phone; 
dateTimePickerDoB.Value = s.dob; 
textBoxDepartment.Text = s.department; 
textBoxSalary.Text = s.salary.ToString(); 
} 
} 
catch 
{ 
label6.Text = "An error has occured"; 
} 
} 

Use BinaryFormatter to deserialize the object from file "serialization.binary". After the object is deserialized update the values of text boxes.


That's all for today friends,GoodBye Friend's :-

Guys I hope you like this tutorial or this tutorial is helpful to you, then please Share it with your friends……...

Leave a Comment below friends If you guys have some questions to ask,we will give your answers soon....


Reactions

Post a Comment

0 Comments