Bytes ----------------------------------------------------------------------------------------------------------------------------
When talking about the value members in the serialization and deserialization classes, we also know that the member in the class is not Dangdang, but only the value member and the reference type, now we will introduce how to serialize referenced types, non-sequent files, and serialized files in the SOAP format.
Serialization reference type
By default, a serialization class requires all its members to be serializable (except for events, delegates, and explicit definitions that cannot be serialized)
Now I want to transform the previous example. The example is as follows:
[Serializable] public class MyClass {public string Name {get; set;} public int Age {get; set;} public Work MyWork {get; set ;}} [Serializable] public class Work {public string Name {set; get;} public string Address {set; get;} is actually very simple, add all the class members in the MyClass class to the [Serializable] attribute ,. NET will traverse all the members in MyClass and intelligently skip the loop reference of the class. If the Work type in MyClass does not contain the [Serializable] attribute, that is, the Work cannot be serialized. NET will report an error.
Member not serializable
If you have a member of The SqlConnection type in MyClass, we all know that SqlConnection manages database connections and is difficult to serialize. Therefore, we cannot serialize SqlConnection during design, we need to add the [NonSerialized] attribute to the SqlConnection member. NET class. When deserialization is performed, SqlConnection is null. For example:
[Serializable] public class MyClass {public string Name {get; set;} public int Age {get; set;} [NonSerialized] Work myWork; public Work MyWork {get {return myWork;} set {myWork = value ;}} public class Work {public string Name {set; get;} public string Address {set; in this way, the Work object will not be serialized into the file stream. When deserialized, The MyWork attribute will be null.
Serialize a SOAP File
To serialize to SOAP, you must first reference the SoapFormatter class in the System. Runtime. Serialization. Formatters. Soap namespace. SoapFormatter is a Soap formatter.
Example:
[Serializable] public class MyClass {public string Name {get; set;} public int Age {get; set;} public Work MyWork {get; set ;}} [Serializable] public class Work {public string Name {set; get;} public string Address {set; get;} class Program {static void Main (string [] args) {MyClass my = new MyClass (); my. name = "Fengjie"; my. age = int. maxValue; Work work = new Work (); work. address = "your next door"; work. name = "unknown"; my. myWork = work; // here the new is the System in the Soap format. runtime. serialization. IFormatter formatter = new SoapFormatter (); // create a file Stream stream = new FileStream (@ "c: \ MyClass. xml ", FileMode. create, FileAccess. write); using (stream) {// The formatter is serialized here. serialize (stream, my );}}}
In this way, the "Fengjie" object is serialized in the SOAP format and generated into your drive C (you can be depressed). The file content is as follows:
SOAP-ENV: Envelope xmlns: xsi =" http://www.w3.org/2001/XMLSchema -Instance "xmlns: xsd =" http://www.w3.org/2001/XMLSchema "Xmlns: SOAP-ENC =" http://schemas.xmlsoap.org/soap/encoding/ "Xmlns: SOAP-ENV =" http://schemas.xmlsoap.org/soap/envelope/ "Xmlns: clr =" http://schemas.microsoft.com/soap/encoding/clr/1.0 "SOAP-ENV: encodingStyle =" http://schemas.xmlsoap.org/soap/encoding/ "> <SOAP-ENV: Body> <a1: MyClass id =" ref-1 "xmlns: a1 =" http://schemas.microsoft.com/clr/nsassem/ConsoleApplication1/ConsoleApplication1%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull "> <_ X003C_Name_x003E_k _ BackingField id =" ref-3 "> Fengjie </_ x003C_Name_x003E_k _ BackingField> <_ blank _ BackingField> 2147483647 </_ blank _ BackingField> <_ x003C_MyWork_x003E_k _ BackingField href = "# ref-4"/> </a1: myClass> <a1: Work id = "ref-4" xmlns: a1 =" http://schemas.microsoft.com/clr/nsassem/ConsoleApplication1/ConsoleApplication1%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull "> <_ X003C_Name_x003E_k _ BackingField id =" ref-5 "> unknown </_ x003C_Name_x003E_k _ BackingField> <_ blank _ BackingField id =" ref-6 "> next door to your home </_ x003C_Address_x003E_k _ BackingField> </a1: work> </SOAP-ENV: Body> </SOAP-ENV: Envelope> serialized file in SOAP format has a certain degree of readability, so that you can better cross-platform deserialization of the image. However, in terms of speed, it is still faster to use binary files, but it is difficult to perform cross-platform deserialization for binary files. Each has its own advantages.
This section is complete. In the next section, we will explain how to use the generic formatter to serialize the object.
Author: "humane programming"