Examples of C # serialization and deserialization (Serialize,deserialize)

Source: Internet
Author: User

This article mainly introduces C # serialization and deserialization (Serialize,deserialize) methods, examples of C # serialization and deserialization of common techniques, the need for friends can refer to the following

The examples in this paper describe the implementation of C # serialization and deserialization (Serialize,deserialize). Share to everyone for your reference. The specific analysis is as follows:

If you want to save the data that runs the program, either save it to the database, create a new normal file, and then save the data in. But there's a downside to both, The structure of the original data cannot be saved. For example, the field values in a class are saved and then read out before they can be parsed. Serialization technology saves you from the process of parsing. Save and then read it directly to get a class

There are three ways to serialize: Binaryformatter,soapformatter,xmlserializer

1.BinaryFormatter

Saved as a binary data stream. Usage Examples:

?
1234567891011 using System.IO;using System.Runtime.Serialization.Formatters.Binary;[Serializable]//如果要想保存某个class中的字段,必须在class前面加个这样attribute(C#里面用中括号括起来的标志符)public class Person{public int age;public string name;[NonSerialized] //如果某个字段不想被保存,则加个这样的标志public string secret;}

Serialization:

?
12345678910111213 classProgram{ staticvoid Main(string[] args){Person person = newPerson();person.age = 18;person.name = "tom";person.secret = "i will not tell you";FileStream stream =newFileStream(@"c:\temp\person.dat",FileMode.Create);BinaryFormatter bFormat =newBinaryFormatter();bFormat.Serialize(stream, person);stream.Close();}

Deserialization:

?
12345678910111213 classprogram { staticvoid Main ( string [] args { person person = Newperson (); filestream stream =newfilestream ( @ "c:\temp\ Person.dat " ,filemode.open); binaryformatter bformat =newbinaryformatter (); person = (person) bformat.deserialize (stream); //deserializes the resulting object. The type conversion must be done stream. Close (); console.writeline (person.age + person.name + person.secret); //result is 18tom. Because Secret has not been serialized.

2.SoapFormatter

Save the data as an XML file. There is some additional soap information in addition to the contents of the save. It is used as BinaryFormatter. Just replace the BinaryFormatter with SoapFormatter.

Change the file name to Person.xml

The other is adding namespaces: using System.Runtime.Serialization.Formatters.Soap;
The name of the air conditioner on the assembly sometimes vs is not automatically referenced. You have to refer to it manually. Select project, right-click to select Add Reference. Under the. NET tab

System.Runtime.Serialization.Formatters.Soap. Then click OK.

Supplement: The Simple Object Access Protocol (PROTOCOL) is a simple protocol for exchanging information in a decentralized or distributed environment, and is an XML-based protocol that consists of four parts: the SOAP Package (envelop), The wrapper defines a framework that describes what the content in the message is, who sent it, who should accept and handle it, and how to handle it; the SOAP encoding rule (encoding rules), which represents an instance of the data type that the application needs to use; SOAP RPC Representation (RPC representation), which represents the contract for remote procedure calls and replies; A SOAP binding (binding) that uses the underlying protocol to exchange information.

3.XmlSerializer

is also saved as an XML file. But there is no additional information. It can only hold fields of the public type. While the other two types can hold fields of the type.
The person class above is still used here.

To add a namespace:

?
12 usingSystem.IO;usingSystem.Xml.Serialization;

Serialization:

?
12345678910111213 classProgram{ staticvoid Main(string[] args){Person person = newPerson();person.age = 18;person.name = "tom";person.secret = "i will not tell you";FileStream stream =newFileStream(@"c:\temp\xmlFormat.xml",FileMode.Create);XmlSerializer xmlserilize = newXmlSerializer(typeof(Person));xmlserilize.Serialize(stream, person);stream.Close();}

Deserialization:

?
1234567891011 classprogram { staticvoid Main ( string [] args { person person = Newperson (); filestream stream =newfilestream ( @ "c:\temp\ Xmlformat.xml " ,filemode.open); xmlserializerxmlserilize = Newxmlserializer ( typeof person = (person) xmlserilize. Deserialize (stream); stream. Close (); console.writeline (person.age + person.name + person.secret);

 

C # serialization and deserialization (Serialize,deserialize) instance detailed

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.