C # enhanced series Article 7: serialization and deserialization

Source: Internet
Author: User

We may often hear about serialization and deserialization. In fact, a simple explanation is that serialization stores an object in a file or database field, deserialization is to convert the file to the original object at an appropriate time.
I think the main functions are:
1. Read the information of the last saved object at the next startup of the process.
2. Transmit data between different AppDomains or processes
3. Data Transmission in Distributed Application Systems
......
In C #, there are also three common serialization Methods: BinaryFormatter, SoapFormatter, and XML serialization.
This article uses a small example to illustrate the specific usage and similarities of the three methods.

This example uses three different methods to serialize and deserialize a Book object. Of course, this Book class can be serialized first. For how to serialize a class, see: C # enhanced series Article 1: Using ViewState and talking about serialization
 
Book class
This class is relatively simple. It defines some public fields and a read/write attribute, a private field, and a field marked as [NonSerialized], which will be embodied in the following example.

I. BinaryFormatter serialization Method
1. serialization is to assign a value to the Book class and then serialize it to a file.
 
Book book = new Book ();
Book. BookID = "1 ";
Book. alBookReader. Add ("gspring ");
Book. alBookReader. Add ("Yongchun ");
Book. strBookName = "C # enhancement ";
Book. strBookPwd = "*****";
Book. SetBookPrice ("50.00 ");
BinarySerialize serialize = new BinarySerialize ();
Serialize. Serialize (book );
2. deserialization
 
BinarySerialize serialize = new BinarySerialize ();
Book book = serialize. DeSerialize ();
Book. Write ();
3. Testing
 
BinarySerialize class
It mainly calls System. runtime. serialization. formatters. the BinaryFormatter class in Binary space is serialized and deserialized, and written to a file in a thumbnail Binary format, which is faster, in addition, the written files are saved in binary format with a certain confidentiality effect.
After deserialization is called:

That is to say, all the Members marked as NonSerialized can be serialized.

Ii. SoapFormatter serialization Method
The method for calling serialization and deserialization is similar to the above. I will not list it. Let's take a look at the SoapSerialize class.
 
SoapSerialize class
It mainly calls System. runtime. serialization. formatters. the SoapFormatter class in the Soap space is serialized and deserialized, and the System must be applied before use. runtime. serialization. formatters. soap. dll (. net)
The serialized file is a file in the Soap format (Simple Object Access Protocol (SOAP). It is a lightweight, Simple, XML-based Protocol, it is designed to exchange structured and solidified information on the WEB. SOAP can be used in combination with many existing Internet protocols and formats, including Hypertext Transfer Protocol (HTTP), Simple Mail Transfer Protocol (SMTP), and multi-purpose Internet Mail Extension protocol (MIME ). It also supports a large number of applications from the message system to Remote Process calling (RPC. SOAP uses a combination of XML-based data structures and Hypertext Transfer Protocol (HTTP) to define a standard method to use distributed objects in different operating environments on the Internet .)
The result after deserialization is called is the same as method 1.

Iii. XML serialization Method
The method for calling serialization and deserialization is similar to the above. I will not list it. Let's look at the XmlSerialize class.
 
XmlSerialize class
From the three test classes, we can see that the calling methods of the three methods are similar, but the classes used are different.
The xml serialized file is a common xml file:
 
Book. xml
The output is as follows:

That is to say, in xml serialization, only public fields and read/write attributes can be saved, and private fields cannot be serialized.

Circular references:
For example, add the following attribute to the Book class in the above example:
Public Book relationBook;
Use the following method when calling serialization:
 
Book book = new Book ();
Book. BookID = "1 ";
Book. alBookReader. Add ("gspring ");
Book. alBookReader. Add ("Yongchun ");
Book. strBookName = "C # enhancement ";
Book. strBookPwd = "*****";
Book. SetBookPrice ("50.00 ");

Book book2 = new Book ();
Book2.BookID = "2 ";
Book2.alBookReader. Add ("gspring ");
Book2.alBookReader. Add ("Yongchun ");
Book2.strBookName = ". NET enhancement ";
Book2.strBookPwd = "*****";
Book2.SetBookPrice ("40.00 ");

Book. relationBook = book2;
Book2.relationBook = book;
BinarySerialize serialize = new BinarySerialize ();
Serialize. Serialize (book );
In this case, loop reference occurs, and BinarySerialize and SoapSerialize can be serialized normally (.. NET). If XmlSerialize encounters this situation, an error is returned: "SerializableTest. circular reference is detected when the Book object is located. "

Author: "Computer Encyclopedia (only used for technology .."

Related Article

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.