What is Serialization in C#?
Serialization in C# is the process of bringing an object into a form that it can be written on stream. 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 in C#?
As the name suggests, deserialization in C# is the reverse process 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
Example
Here I will be giving you an example of how to serialize and deserialize an object using binary formatter or xml formatter.
Create a new Windows Form Application and add few controls to it as shown below.

Now make a class named Employee:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
[Serializable]
public class Employee
{
private string Name;
public string name
{
get
{
return Name;
}
set
{
Name = value;
}
}
private string Phone;
public string phone
{
get
{
return Phone;
}
set
{
Phone = value;
}
}
private DateTime DoB;
public DateTime dob
{
get
{
return DoB;
}
set
{
DoB = value;
}
}
private string Department;
public string department
{
get
{
return Department;
}
set
{
Department = value;
}
}
private int Salary;
public int salary
{
get
{
return Salary;
}
set
{
Salary = value;
}
}
[NonSerialized]
public string additionalInfo;
}
}
Put [Serializable] on top of the class. For those attributes which you don't want to serialize put [NonSerialized] on them.
Add a click event for Serialize button. When clicking on the serialize button we want to serialize the object of class Employee and store it in a file named "employee.binary".
private void Serialize_Click(object sender, EventArgs e)
{
Employee emp = new Employee {
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 bf = new BinaryFormatter();
FileStream fsout = new FileStream("employee.binary", FileMode.Create, FileAccess.Write, FileShare.None);
try
{
using (fsout)
{
bf.Serialize(fsout, emp);
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 "employee.binary". In this file your serialized object will be stored. bf.Serialize(fsout, emp) will serialize the object "emp" and store it in file "employee.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.
private void Deserialize_Click(object sender, EventArgs e)
{
Employee emp = new Employee();
BinaryFormatter bf = new BinaryFormatter();
FileStream fsin = new FileStream("employee.binary", FileMode.Open, FileAccess.Read, FileShare.None);
try
{
using (fsin)
{
emp = (Employee) bf.Deserialize(fsin);
label6.Text = "Object Deserialized";
textBoxName.Text = emp.name;
textBoxPhone.Text = emp.phone;
dateTimePickerDoB.Value = emp.dob;
textBoxDepartment.Text = emp.department;
textBoxSalary.Text = emp.salary.ToString();
}
}
catch
{
label6.Text = "An error has occured";
}
}
Use BinaryFormatter to deserialize the object from file "employee.binary". After the object is deserialized update the values of text boxes.
Now let's execute our program and add some values to text boxes as shown below.

Click on Serialize.

The message says that the object is serialized. To make sure that it is serialized go to,
WindowsFormsApplication\WindowsFormsApplication\bin\Debug\employee.binary
Here you will find the file.

Now to deserialize the object, remove the text from the text boxes and change the date to something else.

Click on Deserialize and you will get the following.

Serialize object in XML format,
For xml serialization use XmlSerializer instead of BinaryFormatter.
XmlSerializer xs = new XmlSerializer(typeof(Employee));
Change the filename from "employee.binary" to "employee.xml" and use [Xmlgnore] instead of [NonSerialized] in Employee class. The rest will be the same.
FredPosted Dec 22, 2025, 10:33 AM
Hi, could you or anyone please confirm if, few months ago pointed by this very URL, there was a ser/des example written in C# specific for Json? And if yes is any way possible to get it back?
Charles HildebrantPosted Jul 7, 2023, 5:58 PM
Hi, Fahad, to get this to work I had to suppress a warning: #pragma warning disable SYSLIB0011 // Type or member is obsolete bf.Serialize(fsout, employee); #pragma warning restore SYSLIB0011 // Type or member is obsolete You wrote this just last month but the files shown are from 2016. What version of .net did you use? I'll research an update. Cheers Charles
Anand NavalePosted Nov 29, 2019, 4:20 AM
Good One...!
Sweety KumariPosted Oct 5, 2018, 4:36 AM
Hi pls anyone explain me private void Serialize_Click(object sender, EventArgs e) { Employee emp = new Employee { name = textBoxName.Text, phone = textBoxPhone.Text, dob = dateTimePickerDoB.Value, department = textBoxDepartment.Text, ... Doesnt look like constructor. Then what use of this code ?TIA
Hitanshi MehtaPosted Sep 28, 2018, 6:27 PM
Good one!!!!!!
Chaithanya ShivaPosted Feb 1, 2018, 9:17 AM
This article is good but if you enter multiple values the old values are getting overriden by new values
VIKAS KUMARPosted Nov 22, 2016, 4:42 AM
Nice and very effective of explaining.
Maaz MahmoodPosted Jul 1, 2016, 4:05 PM
Good Way
ibrahim shaikPosted Jun 28, 2016, 11:08 AM
Can you pls extend the article for json serialization as well.Thanks in Advance...
Vignesh ManiPosted Jun 28, 2016, 8:28 AM
Nice
Suresh MolabantiPosted Jun 28, 2016, 5:49 AM
It's awesome thank you
Suresh MolabantiPosted Jun 28, 2016, 5:48 AM
Bro it is superb...........
Samir BhogaytaPosted Jun 27, 2016, 5:55 AM
It's nice one Fahad.....Thanks
Basil HowitsonPosted Jun 27, 2016, 4:43 AM
Thank you Fahad. This is a very clear and understandable explanation that actually makes sense to me.