×

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
}

Related Topics

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.

Dictionary in C#

C #: We can also pronounce C # programming language as "C-Sharp programming language. It is a Microsoft-developed object-oriented programming language that utilizes the.NET Framework. C # is related to other widely...

3 minutes read.

Switch Statements in C#

In C#, the switch case statement checks one variable for various possible values (cases) and executes the case which is true. Switch case statements are mainly used in menu driven...

3 minutes read.

C# static field

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only...

5 minutes read.

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.

C# Structs

In C#, Structs are like classes but they are used to create light weight objects like color, Rectangle, etc. Structs are value type unlike classes that are reference type. Using...

3 minutes read.

C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

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...

4 minutes read.

C# Variables

A variable is a symbol used to store user’s data. It represents a memory location. Its value can be changed multiple times and can be reused many times.Example: int a; //integer...

1 minute read.

C# do While loop

C# do while loop is responsible for executing a part of the code several number of times. It is an exit controlled loop which is recommended to use if the...

3 minutes read.

C# Function call by value

When you call the function by value, the copy of the actual value is passed in the function instead of its reference. The following example includes the function call by...

1 minute read.

C# Sealed

C# provides sealed keyword to apply restrictions on class and methods. If a class is defined as sealed then it can’t be inherited. If a method is defined as sealed...

3 minutes read.

C# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 minutes read.