Serialization is the process of converting a object into binary format and store data in a text file
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace SerializeExample
{
public partial class Form1 : Form
{
Employee E1 = new Employee();
FileStream fs ;
BinaryFormatter b = new BinaryFormatter();public Form1()
{
InitializeComponent();
}private void Form1_Load(object sender, EventArgs e)
{}
private void button1_Click(object sender, EventArgs e)
{
E1.Eno = int.Parse(textBox1.Text);
E1.Ename = textBox2.Text;
fs = new FileStream(“E:\\Employee.txt”, FileMode.Create, FileAccess.Write);
b.Serialize(fs, E1);
fs.Flush();
fs.Close();
MessageBox.Show(“Serialized”);
textBox1.Text = “”;
textBox2.Text = “”;
}private void button2_Click(object sender, EventArgs e)
{
fs = new FileStream(“E:\\Employee.txt”, FileMode.Open, FileAccess.Read);
E1 = (Employee)b.Deserialize(fs);
fs.Flush();
fs.Close();
textBox4.Text = E1.Eno.ToString();
textBox3.Text = E1.Ename;
MessageBox.Show(“Deserialized”);
}
}
}