C three serialization methods are Shared

  • 2020-06-15 10:06:06
  • OfStack

Serialization is the process of converting an object to a byte stream for long-term storage in memory, a database, or a file. Its main purpose is to save the state of the object for later use. The opposite process is called deserialization.

Serialize 1 object
To serialize an object, we need a serialized object, a (byte) stream that holds the serialized object, and a formatter. Let's look at the System.Runtime.Serialization namespace before serializing. The ISerializable interface allows us to make any class serializable.

If we identify the [Serializable] feature in our written classes, we can serialize these classes. Serialization serializes all members of a class unless the members of the class are marked [NonSerializable].

Serialized type
The & # 8226; Serialization in base 2
The & # 8226; SOAP serialization
The & # 8226; XML serialization
Base 2 (stream) serialization:
Base 2 (stream) serialization is a mechanism for writing data to an output stream so that it can be used to automatically reconstitute the corresponding object. Base 2, the name implies that the necessary information is stored in the storage medium, and that the necessary information requires the creation of an exact base 2 copy of an object. In base 2 (stream) serialization, the state of the entire object is saved, whereas in XML serialization only part of the data is saved. In order to use the serialization, we need to introduce System. Runtime. Serialization. Formatters. Binary namespace. The following code USES serialization BinaryFormatter class. NET string type of object.


using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace SerializationTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //Serialization of String Object          
            string strobj = "test string for serialization";
            FileStream stream = new FileStream("C:\\StrObj.txt", FileMode.Create, FileAccess.Write ,
            FileShare.None);
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, strobj);
            stream.Close();

            //Deserialization of String Object
            FileStream readstream = new FileStream("C:\\StrObj.txt", FileMode.Open , FileAccess.Read ,
            FileShare.Read );
            string readdata = (string)formatter.Deserialize(readstream);
            readstream.Close();
            Console.WriteLine(readdata);
            Console.ReadLine();

        }
    }
} 

SOAP serialization:
The SOAP protocol is an ideal choice for information interaction between heterogeneous applications. . We need in the application to add System Runtime. Serialization. Formatters. Soap namespace so that the. Used in Net SOAP serialization. The main advantage of SOAP serialization is portability. SoapFormatter serializes objects into SOAP messages or parses SOAP messages and reconstructs the serialized objects. The following code USES the SoapFormatter class in.Net to serialize the object of the string class.


using System; 
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap ;

namespace SerializationTest
 {
    class Program
    {
        static void Main(string[] args)
        {
            //Serialization of String Object            
            string strobj = "test string for serialization";
            FileStream stream = new FileStream("C:\\StrObj.txt", FileMode.Create, FileAccess.Write ,
            FileShare.None);
            SoapFormatter formatter = new SoapFormatter();
            formatter.Serialize(stream, strobj);
            stream.Close();
            //Deserialization of String Object
            FileStream readstream = new FileStream("C:\\StrObj.txt", FileMode.Open , FileAccess.Read ,
            FileShare.Read );
            string readdata = (string)formatter.Deserialize(readstream);
            readstream.Close();
            Console.WriteLine(readdata);
            Console.ReadLine();
        }
    }
} 

XML serialization:
According to MSDN, "XML serialization converts (serializes) the public fields and properties of an object or parameter and the return values of a method into an XML stream that complies with the XSD documentation standards. Because XML is an open standard, XML can be handled by any program needed, regardless of the platform, so XML serialization is used in strongly typed classes with exposed properties and fields, and its occurrence and fields are converted to the serialized format (in this case XML) for storage or transmission.

We must add System.XML.Serialization references to use XML serialization. The basis for using XML serialization is XmlSerializer. The following code USES the XmlSerializer class in.Net to serialize the string object.


using System;
using System.IO;
using System.Xml.Serialization;

namespace SerializationTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //Serialization of String Object            
            string strobj = "test string for serialization";
            FileStream stream = new FileStream("C:\\StrObj.txt", FileMode.Create, FileAccess.Write ,
            FileShare.None);
            XmlSerializer  xmlserializer = new XmlSerializer(typeof(string));
            xmlserializer.Serialize(stream, strobj);
            stream.Close();

 
            //Deserialization of String Object
            FileStream readstream = new FileStream("C:\\StrObj.txt", FileMode.Open , FileAccess.Read ,
            FileShare.Read );
            string readdata = (string)xmlserializer.Deserialize(readstream);
            readstream.Close();
            Console.WriteLine(readdata);
            Console.ReadLine();

        }
    }
} 

What is a formatter?
A formatter is used to determine the sequence format of an object. Their purpose is to serialize an object into a suitable format before transferring it over the network. They provide the IFormatter interface. Two formatting classes are provided in.NET: BinaryFormatter and SoapFormatter, which both inherit from the IFormatter interface.

Use serialization
Serialization allows developers to save the state of an object and refactor the object if needed, while supporting object storage and data exchange well. With serialization, developers can use Web Service to send objects to remote applications, transfer objects from one domain to another, transfer objects in XML format and be able to pass through firewalls, or maintain security or user-specific information between applications, and so on.


Related articles: