If you want to save the data in the running procedure either to the database, or create a new common file, and then save the data. however, the two have the disadvantage that they cannot store the original data structure. for example, the field values in a class are saved and then read and parsed. serialization technology saves you the parsing process. after saving and then reading it, you will get a class
There are three serialization Methods: binaryformatter, soapformatter, and xmlserializer.
1. binaryformatter
Save as a binary data stream. Usage example:
Using system. IO;
Using system. runtime. serialization. formatters. Binary;
[Serializable] // if you want to save a field in a class, you must add this attribute before the class (C # contains a symbol enclosed in brackets)
Public class person
{
Public int age;
Public string name;
[Nonserialized] // If a field does not want to be saved, add such a flag.
Public String secret;
}
Serialization:
Classprogram
{
Staticvoid
Main (string [] ARGs)
{
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:
Classprogram
{
Staticvoid
Main (string [] ARGs)
{
Person = newperson ();
Filestream stream = newfilestream (@ "C: \ temp \ person. dat", filemode. Open );
Binaryformatter bformat = newbinaryformatter ();
Person = (person) bformat. deserialize (Stream); // deserialization produces an object. type conversion is required.
Stream. Close ();
Console. writeline (person. Age + person. Name + person. Secret); // The result is 18tom. Because secret is not serialized.
}
2. soapformatter
Save the data as an XML file. In addition to the saved content, there are some additional soap information. Its usage is the same as that of binaryformatter. You only need to replace binaryformatter with soapformatter.
Change the file name to person. xml.
Add the namespace: using system. runtime. serialization. formatters. Soap;
This name is used by the air conditioner. Sometimes vs is not automatically referenced. You must manually reference it. Select the project, right-click Add reference, and select under the. NET label.
System. runtime. serialization. formatters. Soap. Click OK.
Supplement: The Simple Object Access Protocol (SOAP) is a simple protocol for exchanging information in a distributed or distributed environment. It is an XML-based protocol that consists of four parts: the soap encapsulation (envelop) defines a framework that describes the content in a message, who sent the message, who should accept and process it, and how to process it; the soap Encoding Rules (encoding rules) are used to represent the Data Type instances required by the application; the soap RPC representation (RPC representation) indicates the protocols for remote process calls and responses; soap binding, which uses the underlying protocol to exchange information.
3. xmlserializer
It is also saved as an XML file, but there is no additional information. In addition, it can only save public fields. The other two types can save fields of this type.
The above person class is still used here.
Add a namespace:
Using system. IO;
Using system. xml. serialization;
Serialization:
Classprogram
{
Staticvoid
Main (string [] ARGs)
{
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:
Classprogram
{
Staticvoid
Main (string [] ARGs)
{
Person = newperson ();
Filestream stream = newfilestream (@ "C: \ temp \ xmlformat. xml", filemode. Open );
Xmlserializerxmlserilize
= Newxmlserializer (typeof (person ));
Person = (person) xmlserilize. deserialize (Stream );
Stream. Close ();
Console. writeline (person. Age + person. Name + person. Secret );
}