Create Object serialization files-Comparison of soapformatter, binaryformatter, and xmlserializer serialization files

Source: Internet
Author: User

Serialization itself refers to converting objects into data streams, which facilitates storage and transmission.

Soapformatter and binaryformatter are both formatter. I thought xmlserializer also inherited the iformatter interface. Later I found that it is not.

For details about formatter and iformatter, go to the namespace system. runtime. serialization. formatters.

Usage comparison

// Create a file stream to write the file
Filestream = new filestream ("dosum. bin", filemode. Create );
// Use the CLR binary formatter
Binaryformatter = new binaryformatter ();
// Serialize to disk
Binaryformatter. serialize (filestream, sobj );
Filestream. Close ();

Xmlserializer is different

// Create a file stream to write the file
Filestream = new filestream ("dosum. xml", filemode. Create );
// Use the CLR binary formatter
System. xml. serialization. xmlserializer
Formatter = new xmlserializer (typeof (sumof ));
// Serialize to disk
Formatter. serialize (filestream, sobj );
Filestream. Close ();

You can use (defined) Different formatters to create Object serialization.

For example, soapformatter and binaryformatter

Of course, you can also inherit a formatter to define your own formatter.

Soapformatter and binaryformatter cannot be inherited. they implement the iformatter interface, while xmlserializer does not implement the iformatter interface, but we canThe definition class inherits from it.

Serialization of exported content.

If we want to control the serialized content, we have two ways to control it. One is to define our own formatter, and the other is to implement the serialization object interface system. runtime. serialization. iserializable.

There is a method in the formatter:

Public override void serialize (system. Io. Stream serializationstream, object graph)

There is a method in iserializable:

Public void getobjectdata (system. runtime. serialization. serializationinfo info, system. runtime. serialization. streamingcontext context)

It should be noted that, even though the object implements iserializable, the content exported using xmlserializer remains unchanged.

Note the following text

Serialization procedure
When the serialize method is called on the formatting program, Object serialization follows the following rules:
Check whether the formatting program has a proxy selector.If yes, check whether the proxy Selector Processes objects of the specified type.. If the Selector Processes this object type, iserializable. getobjectdata is called on the proxy selector.
If no proxy selector is available or you do not process this type, you will check whether the serializable attribute is used to mark the object. If it is not marked, serializationexception is thrown.
If the object has been correctly marked, check whether the object has implemented iserializable. If implemented, getobjectdata is called on the object.
If the object does not implement serializable, the default serialization policy is used to serialize all fields not marked as nonserialized.

For Object serialization, search for this article on the Internet <. Net Object serialization>

 

I have a problem here. When I recently transferred a dataset, can I define a formatter to sequence the sequence file obtained by using datatset. getxml?

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.