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

  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Runtime.Serialization.Formatters.Binary;
Step 2: Usage
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. Employees emps = new Employees();
  4. emps.Add(new Employee("1", "Lajapathy"));
  5. emps.Add(new Employee("2", "Anand"));
  6. emps.Add(new Employee("3", "Sathiya"));
  7. emps.Add(new Employee("4", "Lakshmi"));
  8. emps.Add(new Employee("5", "Parthiban"));
  9. string pth = @"D:\Test.bin";
  10. //Serializing the collection
  11. Serialize(emps, pth);
  12. //Deserializing the collection
  13. Deserialize(pth);
  14. }

Step 3: Serializing the Object using Binary Formatter

  1. //Serializing the List
  2. public void Serialize(Employees emps, String filename)
  3. {
  4. //Create the stream to add object into it.
  5. System.IO.Stream ms = File.OpenWrite(filename);
  6. //Format the object as Binary
  7. BinaryFormatter formatter = new BinaryFormatter();
  8. //It serialize the employee object
  9. formatter.Serialize(ms, emps);
  10. ms.Flush();
  11. ms.Close();
  12. ms.Dispose();
  13. }

Step 4: Deserializing the Object using Binary Formatter

  1. //Deserializing the List
  2. public void Deserialize(String filename)
  3. {
  4. //Format the object as Binary
  5. BinaryFormatter formatter = new BinaryFormatter();
  6. //Reading the file from the server
  7. FileStream fs = File.Open(filename, FileMode.Open);
  8. object obj = formatter.Deserialize(fs);
  9. Employees emps = (Employees)obj;
  10. fs.Flush();
  11. fs.Close();
  12. fs.Dispose();
  13. foreach (Employee employee in emps)
  14. {
  15. Response.Write(employee.Name + "<br/>");
  16. }
  17. }
Step 5: Output Path of Serialization:

serialzation1.gif
Step 6:
Output of Deserialization:

serialzation2.gif

  1. //Deserializing the List
  2. public void Deserialize(String filename)
  3. {
  4. //Format the object as Binary
  5. BinaryFormatter formatter = new BinaryFormatter();
  6. //Reading the file from the server
  7. FileStream fs = File.Open(filename, FileMode.Open);
  8. object obj = formatter.Deserialize(fs);
  9. Employees emps = (Employees)obj;
  10. fs.Flush();
  11. fs.Close();
  12. fs.Dispose();
  13. foreach (Employee employee in emps)
  14. {
  15. Response.Write(employee.Name + "<br/>");
  16. }
  17. }

Copy & Paste Code Snippet

  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. namespace SampleApplication
  6. {
  7. public partial class ObjectSerialization : System.Web.UI.Page
  8. {
  9. protected void Page_Load(object sender, EventArgs e)
  10. {
  11. Employees emps = new Employees();
  12. emps.Add(new Employee("1", "Lajapathy"));
  13. emps.Add(new Employee("2", "Anand"));
  14. emps.Add(new Employee("3", "Sathiya"));
  15. emps.Add(new Employee("4", "Lakshmi"));
  16. emps.Add(new Employee("5", "Parthiban"));
  17. string pth = @"D:\Test.bin";
  18. //Serializing the collection
  19. Serialize(emps, pth);
  20. //Deserializing the collection
  21. Deserialize(pth);
  22. }
  23. //Serializing the List
  24. public void Serialize(Employees emps, String filename)
  25. {
  26. //Create the stream to add object into it.
  27. System.IO.Stream ms = File.OpenWrite(filename);
  28. //Format the object as Binary
  29. BinaryFormatter formatter = new BinaryFormatter();
  30. //It serialize the employee object
  31. formatter.Serialize(ms, emps);
  32. ms.Flush();
  33. ms.Close();
  34. ms.Dispose();
  35. }
  36. //Deserializing the List
  37. public void Deserialize(String filename)
  38. {
  39. //Format the object as Binary
  40. BinaryFormatter formatter = new BinaryFormatter();
  41. //Reading the file from the server
  42. FileStream fs = File.Open(filename, FileMode.Open);
  43. object obj = formatter.Deserialize(fs);
  44. Employees emps = (Employees)obj;
  45. fs.Flush();
  46. fs.Close();
  47. fs.Dispose();
  48. foreach (Employee employee in emps)
  49. {
  50. Response.Write(employee.Name + "<br/>");
  51. }
  52. }
  53. }
  54. //Classes
  55. [Serializable]
  56. public class Employee
  57. {
  58. public Employee(String id, String name)
  59. {
  60. _ID = id;
  61. _Name = name;
  62. }
  63. private String _ID = String.Empty;
  64. private String _Name = String.Empty;
  65. public String ID
  66. {
  67. get
  68. {
  69. return _ID;
  70. set
  71. {
  72. _ID = value;
  73. }
  74. }
  75. public String Name
  76. {
  77. get
  78. {
  79. return _Name;
  80. }
  81. set
  82. {
  83. _Name = value;
  84. }
  85. }
  86. }
  87. [Serializable]
  88. public class Employees : CollectionBase
  89. {
  90. //Constructor
  91. public Employees()
  92. {
  93. }
  94. //Add function
  95. public void Add(Employee objT)
  96. {
  97. this.List.Add(objT);
  98. }
  99. //Indexer
  100. public Employee this[int i]
  101. {
  102. get
  103. {
  104. return (Employee)this.List[i];
  105. }
  106. set
  107. {
  108. this.List.Add(value);
  109. }
  110. }
  111. }
  112. }

Thanks for reading this article. Have a nice day.