. Net serialization and deserialization

Source: Internet
Author: User
Tags net serialization

What is serialization?
The runtime environment of ---.net is used to support the streaming mechanism of user-defined types. It stores the status of the object instance to the stored media. In this process, first convert the Public and Private fields of the object and the name of the class (including the Assembly where the class is located) to the byte stream, and then write the byte stream into the data stream. When the object is deserialized, a copy identical to the original object will be created.
  
Serialization objective:
1. Make the custom object persistent in a certain storage form;
2. transfer an object from one place to another.
  
Essentially, the serialization mechanism is to convert the class value into a general (that is, continuous) byte stream, and then the stream can be written to a disk file or any other streaming target. To write this stream, you must use the serialize and deserialize methods in the classes that implement the iformatter interface.

1. XML serializer. This is ASP. The default method used to send and accept Web Service SOAP requests in. net. Public attributes and members of serialized objects.

2. The object transfer method used by soap serializer. DOTNET remoting. At this time, the transmitted object requires the serializable flag.

3. binaryserializer. Same as 2, but in binary format.

The. NET Framework provides the following two classes:
  
I. binaryformatter
  
Binaryformatter uses a binary Formatter for serialization. You only need to create an instance of the stream and formatting program to be used, and then call the serialize method of the formatting program. The stream and the object instance to be serialized are provided to this call as parameters. All member variables (or even variables marked as private) in the class will be serialized.
  
First, create a class:
[Serializable]
Public class myobject {
Public int n1 = 0;
Public int n2 = 0;
Public String STR = NULL;
}
The serializable attribute explicitly indicates that the class can be serialized. Similarly, we can use the nonserializable attribute to explicitly indicate that the class cannot be serialized.
Then we create an instance of this class, serialize it, and save it to the file for persistence:
Myobject OBJ = new myobject ();
OBJ. n1 = 1;
OBJ. n2 = 24;
OBJ. Str = "some strings ";
Iformatter formatter = new binaryformatter ();
Stream stream = new filestream ("myfile. bin", filemode. Create,
Fileaccess. Write, fileshare. None );
Formatter. serialize (stream, OBJ );
Stream. Close ();
  
It is also very easy to restore an object to its previous state. First, create a formatter and a stream for reading, and then let the formatter deserialize the object.
Iformatter formatter = new binaryformatter ();
Stream stream = new filestream ("myfile. bin", filemode. Open,
Fileaccess. Read, fileshare. Read );
Myobject OBJ = (myobject) formatter. deserialize (fromstream );
Stream. Close ();
  
// The following is proof
Console. writeline ("N1: {0}", obj. N1 );
Console. writeline ("N2: {0}", obj. N2 );
Console. writeline ("str: {0}", obj. Str );
2. xmlserializer uses a binary Formatter for serialization.
Using system. xml. serialization;

Serialization

Xmlserializer xml = new xmlserializer (typeof (TEST ));
Filestream FS = new filestream (@ "C: \ t. xml", filemode. Create );
XML. serialize (FS, t );
FS. Close ();

Deserialization

Filestream FS = new filestream (@ "C: \ t. xml", filemode. Open );
Xmlserializer xml = new xmlserializer (typeof (TEST ));
Test t = (TEST) XML. deserialize (FS );

The differences between the xmlserializer class and the first two mainstream serialization classes are:
1. the serializable attribute is not required. The serializable and nonserializable attributes are ignored, but the xmlignore attributes are used, which is similar to the nonserializable attribute.
2. This class cannot securely access private members. Therefore, you must change Private Members to public members or provide appropriate public features.
3. The serialized class must have a default constructor.
Soapseriable

Using system. runtime. serialization. formatters. Soap;

// Serialization
Soapformatter soap = new soapformatter ();
Filestream FS = new filestream (@ "C: \ test. xml", filemode. Create );
Soap. serialize (FS, new test ());
FS. Close ();
Console. writeline ("succeeded ");
// Deserialization
Filestream fs1 = new filestream (@ "C: \ test. xml", filemode. Open );
Test t = (TEST) soap. deserialize (fs1 );
Console. writeline (T. Password );
Fs1.close ();

Advantages of binary serialization:

1. All class members (including read-only members) can be serialized;

2. excellent performance.

Advantages of XML serialization:

1. Good interoperability;

2. Strict binary dependency is not required;

3. High readability.

By analyzing the code above, we know whether to select the binary serialization method or the XML serialization method is just to select different formatters. You can select a formatter to complete serialization and deserialization based on your actual needs. At the same time, note that the serialization and deserialization functions in the Code are only different in calling the two core functions serialize () and deserialize (), that is, their parameters are different. Therefore, the above Code has completed some of the most basic but important functions, you can apply them to your program, or expand it to meet the specific needs of the program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.