Serialization of window phone objects

Source: Internet
Author: User
Tags object serialization
[Wphone] Object serialization

In WP7, data is often used to save data. You can serialize the object to a stream and then save it to a file.

There are three common serialization Methods: XML, JSON, and datacontract.

The following describes the use of three types of serialization.

First, define a serialization class person

        public class Person        {            public string Name { get; set; }            public int Age { get; set; }        }

1. Use xmlserializer for serialization

The system. xml. serialization library must be referenced.

Person = new person () {name = "BoMo", age = 20}; // start memorystream MS = new memorystream (); xmlserializer xml = new xmlserializer (typeof (person); XML. serialize (MS, person); // key code for XML serialization byte [] arr = Ms. toarray (); Ms. close (); string xmlstring = encoding. utf8.getstring (ARR, 0, arr. length); MessageBox. show (xmlstring. length. tostring (); // XML deserialization memorystream MS2 = new memorystream (encoding. utf8.getbytes (xmlstring); xmlserializer xml2 = new xmlserializer (typeof (person); person Ptest = xml. deserialize (MS2) as person; // key code of XML deserialization ms2.close ();

 

2. Use JSON

The system. servicemodel. Web library must be referenced.

Serialize the object to the stream and convert it to a JSON string.

During deserialization, the string is read to the stream before deserialization.

Person = new person () {name = "BoMo", age = 20}; // serialize string jsonstring; using (var ms = new memorystream ()) {New datacontractjsonserializer (p1.gettype ()). writeobject (MS, person); jsonstring = encoding. utf8.getstring (Ms. toarray (), 0, (INT) ms. length); MessageBox. show (jsonstring. length. tostring ();} // deserialization of using (var ms = new memorystream (encoding. utf8.getbytes (jsonstring) {person desp = (person) New datacontractjsonserializer (typeof (person )). readobject (MS );}

 

3. Use datacontract for serialization

The system. runtime. serialization library needs to be referenced. The steps are similar to those in JSON format.

Person p1 = new person () {name = "BoMo", age = 28}; // datacontract serialize memorystream MS = new memorystream (); datacontractserializer SER = new datacontractserializer (typeof (person); Ser. writeobject (MS, P1); byte [] array = Ms. toarray (); Ms. close (); string _ serializestring = encoding. utf8.getstring (array, 0, array. length); MessageBox. show (_ serializestring. length. tostring (); // deserialize datacontractserializer ser2 = new datacontractserializer (typeof (person); memorystream MS2 = new memorystream (encoding. utf8.getbytes (_ serializestring); person P2 = ser2.readobject (MS2) as person;

 

In the preceding three methods, the maximum string, the slowest speed, the fastest JSON, and the smallest volume are serialized in XML.

JSON is recommended for Object serialization.

Easy to understand, self: http://www.cnblogs.com/bomo/archive/2013/01/05/2845207.html

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.