. Net serialization and deserialization,. net serialization

Source: Internet
Author: User
Tags net serialization

. Net serialization and deserialization,. net serialization

1. serialization deserialization

In C #, if you need to: store objects of classes with complex structures, or transmit objects to remote client programs over the network, serialization is required, deserialization (Serialization & Deserialization)

 

2. BinaryFormattter

In. NET, there are three serial types: BinaryFormatter, SoapFormatter, and XmlSerializer.

Among them, BinaryFormattter is the simplest. It directly serializes or deserializes an Object in binary mode. Its advantage is that it is fast and CAN serial private or protected member, in different versions. . NET is compatible and can be viewed. NET's own life-saving method, of course, the shortcomings will also come, and leave. . NET.

3. serialization

BinaryFormatter ser = new BinaryFormatter ();

MemoryStream MS = new MemoryStream ();

Ser. Serialize (MS, DS );

Byte [] buffer = ms. ToArray ();

MemoryStream: Creates a stream that supports the memory storage zone.

4. deserialization

// Deserialization: Put byte [] data into Stream, and BinaryFormatter deserializes the data in the Stream into an object

MemoryStream MS = new MemoryStream (bytes );

BinaryFormatter ser = new BinaryFormatter ();

DataSetSurrogate dss = ser. Deserialize (MS) asDataSetSurrogate;

5. Summary

For serialization and deserialization, BinaryFormate is used, and the MemoryStream of the normal stream must be used. The difference is:

During serialization, the object is serialized and put into MemoryStream. During deserialization, the byte [] data in MemoryStream is deserialized into an object.

  • SerializeUtil.rar (571 Bytes)

Net serialization and deserialization

I can't see your code. I don't know how to write it. I provide my own serialization method.
Before the class to be serialized, add the [Serializable] Keyword:
// Instantiate MemoryStream
MemoryStream memoryStream = new MemoryStream ();
// Instantiate Formatter
BinaryFormatter formatter = new BinaryFormatter ();
// The serialization class is a base64 string
Formatter. Serialize (memoryStream, this );
// Set the location in the file stream
MemoryStream. Position = 0;
// Deserialization. In your instance, memoryStream is the serialized string.
Formatter. Deserialize (memoryStream );
Deserialize returns an object type, which can be forcibly converted to the original class.
I am using this in practice. If you cannot use it, paste the code.

What are the methods for serialization and deserialization? What are them?

As of. NET 4.0, there are a total of the following serialization classes:
1. xml serialization XmlSerializer
2. binary serialization BinaryFormatter
3. Soap serialization SoapFormatter
4. WCF serialization DataContractSerializer
5. Json serialization DataContractJsonSerializer and JavascriptSerializer

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.