Home > Article > Backend Development > C# Serialization
The following article provides an outline on C# Serialization. The process by which the object instance is converted into a data stream is called serialization and the state of the object instance is converted into data stream because it can be transported across different networks made to be persisted in a location of storage. This serves as an advantage of serialization to transmit the converted data stream across different networks in a format compatible on cross platforms and saves the converted stream data into a medium of storage in a persistent or non-persistent object state so that the same copy can be created in the later time.
Given below are the steps of C# Serialization Object:
An example class to demonstrate [ Serializable ] class:
Code:
[Serializable] public class Check { public int code; public string name; }
Consider the example class below to demonstrate [ NonSerialized() ] attribute:
Code:
[Serializable] public class Check { public int code; public string name; [NonSerialized()] Public double price; }
Given below are the types of serialization that are supported by C#:
Consider the code below to demonstrate the use of XmlAttribute:
Code:
[XmlAttribute("Name")] public string Name { get { return Name; } set { Name = val; } }
Consider the code below to demonstrate the use of XmlSerializer:
Code:
XmlSerializer Serializer = new XmlSerializer(typeof(Prod)); using (TextWriter Writer = new StreamWriter(@"C:\Prod.xml")) { xmlSerializer.Serialize(Writer, prodObject); }
Consider the code below to demonstrate custom Serialization by implementing the ISerializable interface:
Code:
[Serializable] public class Prod : ISerializable { public void GetObjectData(SerializationInfo information, StreamingContext cont) { //Usual code } }
Given below is the example of C# Serialization:
C# program to demonstrate the concept of Serialization.
Code:
using System; using System.IO; using System.Linq; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; //a namespace called demo is created namespace Demo { //Serializable attribute is declared [Serializable] //a class check is defined which will be used for serialization class Check { public int identity; public String nam; static void Main(string[] args) { //an object of the check class is created to serialize it to the file Example.txt Check ob = new Check(); ob.identity = 10; ob.nam = "Shobha"; //a file stream is created IFormatter format = new BinaryFormatter(); Stream stream1 = new FileStream(@"E:\Example.txt",FileMode.Create,FileAccess.Write); //serialization of the object of the class check is done format.Serialize(stream1, ob); stream1.Close(); //a file stream is created stream1 = new FileStream(@"E:\Example.txt",FileMode.Open,FileAccess.Read); //the object of the class check is deserialized Check ob1 = (Check)format.Deserialize(stream1); //the data is written to the console Console.WriteLine(ob1.identity); Console.WriteLine(ob1.nam); Console.ReadKey(); } } }
Output:
In the above program, a namespace called demo is defined. Then a Serializable attribute is defined. A class check is defined to demonstrate the concept of serialization using this class. Two properties identity and nam are defined in the class to which the values 10 and Shobha are assigned respectively. Then an object of the check class is created to serialize it to the file Example.txt. Then a formatter class is defined to convert the object of the class check to a binary stream.
Then a file stream object is created to open the file Example.txt in write mode to write the values of the properties identity and nam into it. Then serialize method is used to transfer the binary data into the text file. Finally, We use deserialize method to deserialize the contents of the text file Example.txt and the data is written to the console as shown in the output snapshot above.
The above is the detailed content of C# Serialization. For more information, please follow other related articles on the PHP Chinese website!