Serialization in C#

Introduction

For storing objects as files, transmitting data across networks, and interacting with databases, serialization is a helpful strategy. It may also be used to streamline communication across various systems whose data structures are stored in various formats. When you use serialization to transfer an object over the network, for instance, the receiving application can deserialize it as necessary.

Using the [Serializable] Attribute in C#

By utilizing the System, you may interact with serialization in.NET and C#. Runtime. Namespace for serialization (i..e, including this namespace in your program). You may instruct the compiler in C# that your type can be serialized by default by using the [Serializable] property. The program below demonstrates how to build a class serializable in C#.

[Serializable]
public class CodeHubEmployee
{
   public int CodeHubEmpId { get; set; }
   public string CodeHubEmpFirstName { get; set; }
   public string CodeHubEmplLastName { get; set; }
}

Types of serialization

  • Binary Serialization

    The process of transforming an item into a stream of bytes is known as binary serialization. After then, this stream may be saved in a file or sent across a network. The stream can be changed back into an object when it is read back into memory. Data storage and transmission may be done extremely effectively via binary serialization. The data’s primary drawback is that it cannot be interpreted by humans. Use an alternative technique, like XML Serialization, if you need to be able to read the data.

    using (FileStream fs = new FileStream("emp.dat", FileMode.Create))
    {
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(fs, myObject);
    }
    emo
    FileStream fs = new FileStream("data.dat", FileMode.Open); 
    BinaryFormatter formatter = new BinaryFormatter(); 
    MyClass myObject = (MyClass)formatter.Deserialize(fs);
  • XML Serialization

    Classes with highly typed fields and attributes are created using XML serialization and are communicated or saved in a serial mode (in this case, in XML). Since XML is an open standard, it doesn’t matter what language or platform software is developed in, it can read an XML stream. To make your class compatible with XML serialization, you must implement two methods.

    GetObjectData() – This function accepts an XmlDictionaryWriter object as an argument and returns void, allowing you to add data into an XML document at runtime. This object contains all the information and settings that we’ll need for the serialization process.

    ReadXml() –
    With the help of this function, an XML document is read into our class once more, allowing us to access its properties just like any other class.

  • JSON Serialization

    The process of transforming an item into a JSON string is known as JSON serialization. Because it is more compact than XML and simpler to grasp than XML, it is widely utilized in applications. When you need to share data across many systems, JSON is a fantastic option because it is supported by all current programming languages. Your objects may be quickly turned into JSON strings so that they can be sent over the network for processing.

  • SOAP Serialization

    Another method for serializing data is the Simple Object Access Protocol or SOAP. When transferring items across apps with different architectures, SOAP serialization might be useful. You ought to mention serialization. Formatters. If you wish to utilize SOAP serialization, include the SOAP namespace in your software.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories