Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location or in the database system. The deserialization is the process which we reverse the serialization process and it is used in the reading of the object into the byte stream of the data.

Serialization in C#

Serialization is the process which we used internally in the remote systematic applications.

Types of Serializability

  • Binary serializability: The Binary serialization is the process where the System.Runtime.Serializabilityis the binary Serializability that includes the name space of the classes. In the programming language the namespace is derived as the gathering of the symbols and identify the objects and refer to the various things.
using System;    // references
using System.IO;     // header files
using System.Runtime.Serialization;   // serialization in running time
using System.Runtime.Serialization.Formatters.Binary;   // in binary format
public class Demo   // class name
 {   
public void SerializeSample()    // method name
{
ClassToSerialize c1 = new ClassToSerialize();  // creating objects
File f = new File("prefech.dat");  // files in terms of creating objects
Stream str = f.Open(FileMode.Create);   // creating file mode
BinaryFormatter bf = new BinaryFormatter();   // creating binary formatter
bf.Serialize(str, c1);
str.Close();
}
public void DeSerializeSample()   // method for deserialization


{
ClassToSerialize c1 = new ClassToSerialize();   // creating the objects 
File f = new File("prefetch.dat");
Stream str = f.Open(FileMode.Open);
BinaryFormatter bf = new BinaryFormatter();
c1 = (ClassToSerialize) bf.Deserialize(str);
Console.WriteLine(c1.name);
str.Close();  // closing the file
}
public static void Main(string[] s)   // main method
{
SerialDemosd = new SerialDemo();
sd.SerializeSample();
sd.DeSerializeSample();
}
}
public class ClassToSerialize 
{
public int id = 200;  // printing id
public string sname = "javatpoint";  // printing sname
}
  • XML Serializability: xml serializability is the process that defined as the any class instances that can be serializable from the xml stream. Basically, the XML serializability is longer compare to the binary files. These files are easy to understand and edit. The XML serializability files accepts the attributes.

         Let's see the syntax for XML serializability

Times New Roman xmlSerializer(moveof(Pop));
using(TextReader Reader = new streamReader(@”c:\xmlserializer)){
XmlSerializer.Serialize(Reader, PopObject)
}
  • SOAP serializability: The soap is defined as the (simple object access protocol) that is used for the XML typed protocol used between the data transmission and the computers. SOAP serialization is the process used to shift the items that is incompatible of one application to another Serializability is runs at the .NET runtime that is the particular class instance          of serialized property in which the class-level serialization.
using system; // reference for each program
     using System.Text;  // it is also known as class name 
     using System.IO;   // header file name
     using System.Runtime.Serialization;  // run time serialization
     using System.collections.Generic;   // generic collections
     using System.Linq;
     using System.Threading.Tasks;   // threading tasks
     using System.Runtime.Serialization.Formatters.Soap; // run time serialization at soap format


namespace soapSerializationExample
{
    class sample    // class name
    {
        static void Main(string[]args)
        {
            Example example = new Example();
example.Name = "javatpoint"; // Instance of our sample class
example.Value =10;
FileStream fileStream1 = new FileStream(@"c:\text\\serialisation.dat",    FileMode.Create); // it creates the Serialization of the object
SoapFormatter formatter1 = new  SoapFormatter();
            formatter1.Serialize(fileStream, example);
        }
    }
}

// let's see the program on serializability in c#

Example 3:

using System;    // references for classes
using System.IO;  // header files
using System.Runtime.Serialization;  // run time serialization
using System.Runtime.Serialization.Formatters.Binary;
public class SerialDemo
{
public void SerializeNow()
{
ClassToSerialize c1= new ClassToSerialize();
c1.SName = "javatpoint";
c1.SAge = 16;
ClassToSerialize.StudentName = "santhosh";  // intializing the names
File fp = new File("prefetch.dat");
Stream str = f.Open(FileMode.Create);  // craeting the file mode
BinaryFormatter bf = new BinaryFormatter();  // creation of Binary formatter
bf.Serialize(str, c1);
str.Close();  // closing file
}
public void DeSerializeNow()
{
ClassToSerialize c1 = new ClassToSerialize(); // creating class serialization
File fp = new File("prefetch.dat"); // file of data
Stream str = f.Open(FileMode.Open);  // creating file mode
BinaryFormatter bf  = new BinaryFormatter();  // binary format creator
c1 = (ClassToSerialize) bf.Deserialize(str);
Console.WriteLine("SName :" + c1.SName);
Console.WriteLine("SAge :" + c1.SAge);
Console.WriteLine("Company SName :" +ClassToSerialize.CompanyName);
Console.WriteLine("Company SName :" + c1.GetSupportClassString());
str.Close(); // closing file
}
public static void Main(string[] s)   // main method
{
SerialDemosd = new SerialDemo();
sd.SerializeNow();
sd.DeSerializeNow();
}
}
public class ClassToSerialize // serialize method
{
private int sage;
private string sname;
static string Studentname;
SupportClasssupprtcls = new SupportClass();
public ClassToSerialize() {
supprtcls.SupportClassString = "In the support class";
}
public int SAge {
set {
return Sage;
}
get {
sage = num;
}
}
public string SName {
get {
return sname;
}
set {
sname = num;
}
}
public static string StudentName
{
set {
return Studentname;
}
get {
Studentname = num;
}
}
public string GetSupportClassString() {
return supprtcls.SupportClassString;  // returning the class
}
}
public class SupportClass  // support class method
{
public string SupportClassString;  // class string vale
}