In this article, we are going to see how to serialize and deserialize an object as binary data using the binary formatter.
Step 1: Used Namespace
- using System;
- using System.Collections;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
Step 2: Usage
- protected void Page_Load(object sender, EventArgs e)
- {
- Employees emps = new Employees();
-
- emps.Add(new Employee("1", "Lajapathy"));
- emps.Add(new Employee("2", "Anand"));
-
- emps.Add(new Employee("3", "Sathiya"));
- emps.Add(new Employee("4", "Lakshmi"));
- emps.Add(new Employee("5", "Parthiban"));
-
- string pth = @"D:\Test.bin";
-
-
- Serialize(emps, pth);
-
-
- Deserialize(pth);
- }
Step 3: Serializing the Object using Binary Formatter
-
- public void Serialize(Employees emps, String filename)
- {
-
- System.IO.Stream ms = File.OpenWrite(filename);
-
-
- BinaryFormatter formatter = new BinaryFormatter();
-
- formatter.Serialize(ms, emps);
- ms.Flush();
- ms.Close();
- ms.Dispose();
- }
Step 4: Deserializing the Object using Binary Formatter
-
- public void Deserialize(String filename)
- {
-
- BinaryFormatter formatter = new BinaryFormatter();
-
-
- FileStream fs = File.Open(filename, FileMode.Open);
-
- object obj = formatter.Deserialize(fs);
- Employees emps = (Employees)obj;
- fs.Flush();
- fs.Close();
- fs.Dispose();
- foreach (Employee employee in emps)
- {
- Response.Write(employee.Name + "<br/>");
- }
- }
Step 5: Output Path of Serialization:
Step 6: Output of Deserialization:
-
- public void Deserialize(String filename)
- {
-
- BinaryFormatter formatter = new BinaryFormatter();
-
-
- FileStream fs = File.Open(filename, FileMode.Open);
-
- object obj = formatter.Deserialize(fs);
- Employees emps = (Employees)obj;
- fs.Flush();
- fs.Close();
- fs.Dispose();
-
- foreach (Employee employee in emps)
- {
- Response.Write(employee.Name + "<br/>");
- }
- }
Copy & Paste Code Snippet
- using System;
- using System.Collections;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
-
- namespace SampleApplication
- {
- public partial class ObjectSerialization : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Employees emps = new Employees();
- emps.Add(new Employee("1", "Lajapathy"));
- emps.Add(new Employee("2", "Anand"));
- emps.Add(new Employee("3", "Sathiya"));
-
- emps.Add(new Employee("4", "Lakshmi"));
- emps.Add(new Employee("5", "Parthiban"));
-
- string pth = @"D:\Test.bin";
-
-
- Serialize(emps, pth);
-
-
- Deserialize(pth);
- }
-
- public void Serialize(Employees emps, String filename)
- {
-
- System.IO.Stream ms = File.OpenWrite(filename);
-
-
- BinaryFormatter formatter = new BinaryFormatter();
-
-
- formatter.Serialize(ms, emps);
- ms.Flush();
- ms.Close();
- ms.Dispose();
- }
-
- public void Deserialize(String filename)
- {
-
- BinaryFormatter formatter = new BinaryFormatter();
-
-
- FileStream fs = File.Open(filename, FileMode.Open);
- object obj = formatter.Deserialize(fs);
- Employees emps = (Employees)obj;
- fs.Flush();
- fs.Close();
- fs.Dispose();
- foreach (Employee employee in emps)
- {
- Response.Write(employee.Name + "<br/>");
- }
- }
- }
-
- [Serializable]
- public class Employee
- {
- public Employee(String id, String name)
- {
- _ID = id;
- _Name = name;
- }
- private String _ID = String.Empty;
- private String _Name = String.Empty;
- public String ID
- {
- get
- {
- return _ID;
-
- set
- {
- _ID = value;
- }
- }
- public String Name
- {
- get
- {
-
- return _Name;
- }
- set
- {
- _Name = value;
- }
- }
- }
- [Serializable]
- public class Employees : CollectionBase
- {
-
- public Employees()
- {
- }
-
-
- public void Add(Employee objT)
- {
- this.List.Add(objT);
- }
-
- public Employee this[int i]
- {
- get
- {
- return (Employee)this.List[i];
- }
- set
- {
- this.List.Add(value);
- }
- }
- }
- }
Thanks for reading this article. Have a nice day.