wp7 序列化

  1 using System.IO;
2 using System.Json;
3 using System.Runtime.Serialization;
4 using System.Runtime.Serialization.Json;
5 using System.Text;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Xml.Serialization;
9
10 namespace SlSerialize
11 {
12 public partial class MainPage : UserControl
13 {
14 Person _person;
15
16 public MainPage()
17 {
18 InitializeComponent();
19 this.Loaded += new RoutedEventHandler(MainPage_Loaded);
20 }
21
22 void MainPage_Loaded(object sender, RoutedEventArgs e)
23 {
24 _person = new Person() { Name = "菩提树下的\"杨过\"", Age = 30 };
25 }
26
27 private void btnJson_Click(object sender, RoutedEventArgs e)
28 {
29 //json序列化开始
30 MemoryStream ms = new MemoryStream();
31 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));
32 ser.WriteObject(ms, _person);
33 byte[] json = ms.ToArray();
34 ms.Close();
35 string jsonString = Encoding.UTF8.GetString(json, 0, json.Length);//序列化得到的字符串
36
37 //json字符串解析(相当于反序列化)
38 JsonValue jsonv = JsonObject.Parse(jsonString);
39 Person pTest = new Person() { Age = int.Parse(jsonv["Age"].ToString()), Name = jsonv["Name"].ToString() };
40
41 //显示结果
42 txtResult.Text = "json序列化后的字符串:(长度" + jsonString.Length + ")\n" + jsonString + "\n\n反序列化后的结果:\nAge=" + pTest.Age + ",Name=" + pTest.Name;
43 }
44
45 private void btnXml_Click(object sender, RoutedEventArgs e)
46 {
47 //xml序列化开始
48 MemoryStream ms = new MemoryStream();
49 XmlSerializer xml = new XmlSerializer(typeof(Person));
50 xml.Serialize(ms, _person);//xml序列化的关键代码
51 byte[] arr = ms.ToArray();
52 ms.Close();
53 string xmlString = Encoding.UTF8.GetString(arr,0,arr.Length);
54 ms.Close();
55
56 //xml反序列化
57 MemoryStream ms2 = new MemoryStream(Encoding.UTF8.GetBytes(xmlString));
58 XmlSerializer xml2 = new XmlSerializer(typeof(Person));
59 Person pTest = xml.Deserialize(ms2) as Person;//xml反序列化的关键代码
60 ms2.Close();
61
62 //显示反序列化后的结果
63 txtResult.Text = "xml序列化后的字符串:(长度" + xmlString.Length + ")\n" + xmlString + "\n\n反序列化后的结果:\nAge=" + pTest.Age + ",Name=" + pTest.Name;
64 }
65
66 private void btnBin_Click(object sender, RoutedEventArgs e)
67 {
68 //DataContract方式序列化
69 MemoryStream ms = new MemoryStream();
70 DataContractSerializer ser = new DataContractSerializer(typeof(Person));
71 ser.WriteObject(ms, _person);
72 byte[] array = ms.ToArray();
73 ms.Close();
74
75 string _serializeString = Encoding.UTF8.GetString(array, 0, array.Length);
76
77 //反序列化
78 DataContractSerializer ser2 = new DataContractSerializer(typeof(Person));
79 MemoryStream ms2 = new MemoryStream(Encoding.UTF8.GetBytes(_serializeString));
80 Person pTest = ser2.ReadObject(ms2) as Person;
81
82 //显示反序列化后的结果
83 txtResult.Text = "DataContract序列化后的字符串:(长度" + _serializeString.Length + ")\n" + _serializeString + "\n\n反序列化后的结果:\nAge=" + pTest.Age + ",Name=" + pTest.Name;
84
85 }
86
87 }
88
89 /// <summary>
90 /// 测试类(需要说明的是在silverlight中不需要加任何的额外标签,比如[DataMember],[Serializeable]之类)
91 /// </summary>
92 public class Person
93 {
94 public string Name { set; get; }
95
96 public int Age { set; get; }
97 }
98 }
99 如果在网络通讯应用(比如socket编程中),最好使用json方式序列化;
100 如果想让最终的xap体积最小(以达到最快加载速度),最好使用DataContractSerializer方式;
101 一般不建议使用XmlSerializer方式处理对象序列化

 

编辑器加载中...

你可能感兴趣的:(序列化)