Serialization is the process of converting complex objects into stream
of bytes for storage.
Deserialization is converting stream of bytes to their original form.
System.IO is the namespace which is used to read and write files.
System.Runtime.Serialization namespace is used for Serialization.
With Object Serialization, we can write any
complex object directly to a disk file and make it impossible for human to
read. In order for the users to read our data files, they have to use our program.
This prevents any anonymous user from open and reading any confidential
information from our text file. .NET framework provide us of Custom-build class
objects to do Serialization.
Practical Exercise
1.
Create a Console class application with name
SerializationApplication
2.
Rename the Program.cs file to
SerializationApplication
3.
Right click the Project folder and create new
class named MyObjSerial
4.
Paste the following coding in the relevant files
5.
Press Control +F5 run the run the application
MyObjSerial.cs
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
namespace MyObjSerial
{
[Serializable()] //Set this attribute to all the classes to be serialized
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set;
}
public int StudentGPA { get; set; }
public
Student(int StuID, string StuName, int StuGPA)
{
StudentId = StuID;
StudentName = StuName;
StudentGPA = StuGPA;
}
public
Student()
{
StudentId = 0;
StudentName = null;
StudentGPA = 0;
}
//Deserialization method.
public void DisplayObjectData()
{
//Get the values from info and assign them to the
appropriate properties
try
{
using (Stream stream = File.Open("data.bin", FileMode.Open))
{
BinaryFormatter
bin = new BinaryFormatter();
var lizards2 = (List<Student>)bin.Deserialize(stream);
foreach (Student student in lizards2)
{
Console.WriteLine("{0}, {1}, {2}",
student.StudentId,
student.StudentName,
student.StudentGPA);
}
}
}
catch (IOException)
{
}
}
//Serialization function.
public void GetObjectData()
{
/*You can use any custom name for your name-value
pair. But you need to ensure to read the values with the same name.
For ex:- If you write StudentId as "StudentId"then you should read
the same with "StudentId"*/
var Students1 = new List<Student>();
Students1.Add(new Student(1,"Shalini",6));
Students1.Add(new Student(2,
"Rex",
5));
try
{
using (Stream stream = File.Open("data.bin", FileMode.Create))
{
BinaryFormatter
bin = new BinaryFormatter();
bin.Serialize(stream, Students1);
}
}
catch (IOException)
{
}
}
}
}
SerializationApplication.cs
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;
namespace MyObjSerial
{
[Serializable()] //Set this attribute to all the classes to be serialized
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set;
}
public int StudentGPA { get; set; }
public
Student(int StuID, string StuName, int StuGPA)
{
StudentId = StuID;
StudentName = StuName;
StudentGPA = StuGPA;
}
public
Student()
{
StudentId =
0;
StudentName = null;
StudentGPA = 0;
}
//Deserialization method.
public void DisplayObjectData()
{
//Get the values from info and assign them to the
appropriate properties
try
{
using (Stream stream = File.Open("data.bin", FileMode.Open))
{
BinaryFormatter
bin = new BinaryFormatter();
var lizards2 = (List<Student>)bin.Deserialize(stream);
foreach (Student student in lizards2)
{
Console.WriteLine("{0}, {1}, {2}",
student.StudentId,
student.StudentName,
student.StudentGPA);
}
}
}
catch (IOException)
{
}
}
//Serialization function.
public void GetObjectData()
{
/*You can use any custom name for your name-value
pair. But you need to ensure to read the values with the same name.
For ex:- If you write StudentId as "StudentId"then you should read
the same with "StudentId"*/
var Students1 = new List<Student>();
Students1.Add(new Student(1,"Shalini",6));
Students1.Add(new Student(2,
"Rex",
5));
try
{
using (Stream stream = File.Open("data.bin", FileMode.Create))
{
BinaryFormatter
bin = new BinaryFormatter();
bin.Serialize(stream, Students1);
}
}
catch (IOException)
{
}
}
}
}
Output
Example 2
Now we shall change the code for both the class files and execute the
program using a different method
MyObjSerial.cs
using System;
using System.IO;
using System.Runtime.Serialization;
using
System.Runtime.Serialization.Formatters.Binary;
namespace MyObjSerial
{
[Serializable()] //Set this attribute to all the classes to be serialized
{
[Serializable()] //Set this attribute to all the classes to be serialized
public class Student : ISerializable
{
public int StudentId;
public string
StudentName;
public int
StudentGPA;
//Default constructor
//Default constructor
public Student()
{
StudentId = 0;
StudentName = null;
{
StudentId = 0;
StudentName = null;
StudentGPA = 0;
}
//Deserialization constructor.
public Student(SerializationInfo
info, StreamingContext ctxt)
{
//Get the values from info and assign them to the appropriate properties
StudentId = (int)info.GetValue("StudentId", typeof(int));
StudentName = (String)info.GetValue("StudentName",
typeof(string));
StudentGPA = (int)info.GetValue("StudentGPA",
typeof(int));
}
//Serialization function.
}
//Serialization function.
public void
GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
/*You can use any custom name for your name-value pair. But you need to ensure to read the values with the same name.
For ex:- If you write StudentId as "StudentId"then you should read the same with "StudentId"*/
info.AddValue("StudentId", StudentId);
info.AddValue("StudentName", StudentName);
info.AddValue("StudentGPA", StudentGPA);
}
}
}
}
}
}
SerializationApplication.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyObjSerial;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace SerializationApplication
{
public class ObjSerial
{
static void Main(string[] args)
{
//Create two Student object
Student st = new Student();
st.StudentId = 1;
st.StudentName = "Shalini";
st.StudentGPA = 6;
// Open a file and serialize the object into it in binary format.
// Open a file and serialize the object into it in binary format.
//
StudentInfo.osl is the file that we create.
// Note:-
you can give any extension you want for your file which is associated with your
program
Stream stream = File.Open("StudentInfo.osl", FileMode.Create);
BinaryFormatter bformatter = new
BinaryFormatter();
Console.WriteLine("Writing
Student Information");
bformatter.Serialize(stream, st);
stream.Close();
//Clear mp for further usage.
st = null;
//Open
the file written above and read values from it.
stream = File.Open("StudentInfo.osl",
FileMode.Open);
bformatter = new BinaryFormatter();
Console.WriteLine("Reading
Student Information");
st = (Student)bformatter.Deserialize(stream);
Console.WriteLine("Student Id: {0}", st.StudentId.ToString());
Console.WriteLine("Student Id: {0}", st.StudentId.ToString());
Console.WriteLine("Student
Name: {0}", st.StudentName);
Console.WriteLine("Student
GPA: {0}", st.StudentGPA);
stream.Close();
}
}
}
stream.Close();
}
}
}
Output
No comments:
Post a Comment