Object serialization in. net (2): Three serialization Methods

Source: Internet
Author: User

The. Net Framework provides serialization methods in 3, which are:
• Binary Serializer
• Soap Serializer
• XML Serializer
1. Binary Serializer
To serialize the object of this class, the class must be marked as serializable. Features required [Serializable] (scope: Class, Struct, Enum, Delegate)
If some variables do not need to be serialized or cannot be serialized, you can use [NonSerialized] (the scope is Field)
Note that if other class attributes are used in the class, if you want them to be serialized, other classes must also be serializable, as shown in the following section:
 
1 namespace SerializationDemo
2 {
3 [Serializable]
4 public class Person
5 {
6 [NonSerialized]
7 private int _ age;
8 public int Age
9 {
10 get
11 {
12 return _ age;
13}
14 set
15 {
16 _ age = value;
17}
18}
19 public DethDisease Disease
20 {
21 get;
22 set;
23}
24 public string Medicine
25 {
26 get;
27 set;
28}
29 public Person ()
30 {
31 Age = 0;
32}
33 public bool Live ()
34 {
35 Age ++;
36 if (Disease! = Null &&! Disease. CanLive (Age, Medicine ))
37 return false;
38 return true;
39}
40}
41}

 
The binary serialization code is mainly the BinaryFormatter.
 
1 IFormatter formatter = new BinaryFormatter ();
2 Stream stream = new FileStream ("Person", FileMode. Create, FileAccess. Write, FileShare. None );
3 formatter. Serialize (stream, newPerson );
4 stream. Close ();

 
 
Deserialization
 
1 IFormatter formatter = new BinaryFormatter ();
2 Stream stream = new FileStream ("Person", FileMode. Open, FileAccess. Read, FileShare. Read );
3 newPerson = (Person) formatter. Deserialize (stream );
4 stream. Close ();

 
 
2. Soap Serializer
Basically the same as above, it only uses different IFormatter implementations, SoapFormatter
 
3. XML Serializer
Use xml Serializer. You do not need to use the [Serializable] feature.
Using xml Serializer, there are some other features to change the xml output results.
 
XmlRoot: Act on the root element and control the output of the root element.
XmlElement: attribute or field Acting on the class
XmlAttribute: output as a property, not a node.
XmlIgnore: No output
XmlText: it must be a string type. Only one of the classes can have this feature. It is neither wrapped nor used as an attribute.
XmlArray: it acts on the array type and can control the output of the array.
Here is the common core method for serializing xml
 
1 public static string XmlSerialize <T> (T obj)
2 {
3 string xmlString = string. Empty;
4 XmlSerializer xmlSerializer = new XmlSerializer (typeof (T ));
5 using (MemoryStream MS = new MemoryStream ())
6 {
7 xmlSerializer. Serialize (MS, obj );
8 xmlString = Encoding. UTF8.GetString (ms. ToArray ());
9}
10 return xmlString;
11}
12 public static T XmlDeserialize <T> (string xmlString)
13 {
14 T t = default (T );
15 XmlSerializer xmlSerializer = new XmlSerializer (typeof (T ));
16 using (Stream xmlStream = new MemoryStream (Encoding. UTF8.GetBytes (xmlString )))
17 {
18 using (XmlReader xmlReader = XmlReader. Create (xmlStream ))
19 {
20 Object obj = xmlSerializer. Deserialize (xmlReader );
21 t = (T) obj;
22}
23}
24 return t;
25}

 
 
Comparison:
The Binary method has the highest efficiency and is less readable than the last two methods.
The Soap method can be used for cross-platform transmission. The xml method is simple and easy to use. It is used internally and flexibly and conveniently.
 
Source code: SerializationDemohttp: // www.bkjia.com/uploadfile/2012/0326/20120326113540865.zip
 

 

From JustRun1983
 

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.