Before exploring C # serialization and deserialization, we must first understand what serialization is, also known as serialization. It is a mechanism used by the. NET runtime environment to support user-defined streaming. Serialization is to save an object to a file or database field. deserialization is to convert the file to the original object when appropriate. The objective is to make the custom object persistent by storage, or to transmit the object from one place to another .. NET Framework provides two serializable methods: 1. Using BinaryFormatter for serialization; 2. Using SoapFormatter for serialization; 3. Using XmlSerializer for serialization. The first method provides a simple binary data stream and some additional types of information, while the second method formats the data stream for XML storage. The third method is similar to the second method in XML format storage, it is much simpler than the second XML format (removes the extra information unique to SOAP ). You can use the [Serializable] attribute to serialize a class. If the elements of a class do not want to be serialized, 1 and 2 can use the [NonSerialized] attribute for flag. 2. You can use the [XmlIgnore] flag.
Next let's get to know more about C # serialization and deserialization:
C # serialization and deserialization 1. Use BinaryFormatter for serialization
The following is a serializable class:
- UsingSystem;
- UsingSystem. Data;
- UsingSystem. Configuration;
- UsingSystem. Web;
- UsingSystem. Web. Security;
- UsingSystem. Web. UI;
- UsingSystem. Web. UI. WebControls;
- UsingSystem. Web. UI. WebControls. WebParts;
- UsingSystem. Web. UI. HtmlControls;
- UsingSystem. IO;
- UsingSystem. Runtime. Serialization. Formatters. Binary;
- /** // <Summary>
- /// Summary of ClassToSerialize
- /// </Summary>
- [Serializable]
- Public ClassClassToSerialize
- {
- Public IntId = 100;
- Public StringName = "Name ";
- [NonSerialized]
- Public StringSex = "male ";
- }
The following describes the serialization and deserialization methods:
- public void SerializeNow()
- {
- ClassToSerialize c = new ClassToSerialize();
- FileStream fileStream =
- new FileStream("c:\temp.dat", FileMode.Create);
- BinaryFormatter b = new BinaryFormatter();
- b.Serialize(fileStream, c);
- fileStream.Close();
- }
- public void DeSerializeNow()
- {
- ClassToSerialize c = new ClassToSerialize();
- c.Sex = "kkkk";
- FileStream fileStream =
- new FileStream("c:\temp.dat",
- FileMode.Open, FileAccess.Read, FileShare.Read);
- BinaryFormatter b = new BinaryFormatter();
- c = b.Deserialize(fileStream) as ClassToSerialize;
- Response.Write(c.name);
- Response.Write(c.Sex);
- fileStream.Close();
- }
Call the above two methods to see the serialized result: the Sex attribute is always null because it is marked as [NonSerialized.
C # serialization and deserialization 2. Use SoapFormatter for serialization
Similar to BinaryFormatter, you only need to make a simple modification:
A. Change. Formatter. Binary in the using statement to. Formatter. Soap;
B. replace all BinaryFormatter with SoapFormatter.
C. Make sure that the file name is. xml.
After the above simple changes, SoapFormatter can be serialized. The generated file is an xml file.
C # serialization and deserialization 3. Use XmlSerializer for serialization
There is another question about the formatter. Suppose we need XML, but we do not want additional information unique to SOAP. What should we do? There are two solutions: either compile a class that implements the IFormatter interface, using a method similar to the SoapFormatter class, but there is no information you don't need; or use the library class XmlSerializer, this class does not use the Serializable attribute, but it provides similar functions.
If we don't want to use the mainstream serialization mechanism, but want to use XmlSeralizer for serialization, we need to modify it as follows:
A. Add System. Xml. Seriali.