.net序列化
在开发过程中,会遇到很多需要使用序列化的场景,比如wcf,webservice或者jquery+.net等。那今天就说说我对序列化的理解。
在.net中有几种序列化的方式,XML、二进制、SOAP、还有JSON。
XML序列化
xml序列化是使用namespace:System.Xml.Serialization下的class:XmlSerializer实现的。序列化之后使用xml格式的。
现有类型
1: [XmlRoot("OnlineOrder")]
2: public class Order
3: {
4: private int orderId;
5:
6: public int OrderId
7: {
8: get { return orderId; }
9: set { orderId = value; }
10: }
11: private DateTime orderTime;
12:
13: [XmlAttribute("OrderServerTime")]
14: public DateTime OrderTime
15: {
16: get { return orderTime; }
17: set { orderTime = value; }
18: }
19:
20: private List<Product> product;
21: public List<Product> Product
22: {
23: get { return product; }
24: set { product = value; }
25: }
26: }
27: public class Product
28: {
29: private int productId;
30:
31: public int ProductId
32: {
33: get { return productId; }
34: set { productId = value; }
35: }
36: private string productName;
37:
38: public string ProductName
39: {
40: get { return productName; }
41: set { productName = value; }
42: }
43:
44:
45: private string productShortName;
46:
47: public string ProductShortName
48: {
49: get { return productShortName; }
50: set { productShortName = value; }
51: }
52: }
53:
执行序列化
1: Order order = new Order
2: {
3: Product = new List<Product>() { new Product() { ProductId = 1, ProductName = "ipone4", ProductShortName = "ip4" } },
4: OrderId = 1001,
5: OrderTime = DateTime.Now
6: };
7:
8: System.IO.StreamWriter writer = new System.IO.StreamWriter("xml.xml");
9:
10: XmlSerializer serializer = new XmlSerializer(typeof(Order));
11: serializer.Serialize(writer, order);
12:
现在可以看看结果了。
<?xml version="1.0" encoding="UTF-8"?>
-<OnlineOrder OrderServerTime="2013-10-27T10:59:49.451353+08:00" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><OrderId>1001</OrderId>-<Product>-<Product><ProductId>1</ProductId><ProductName>ipone4</ProductName><ProductShortName>ip4</ProductShortName></Product></Product></OnlineOrder>
之后修改代码
1:
2: public class Order
3: {
4: private int a = 1;
5: protected int b = 2;
6: public int c = 3;
7: protected int d { get; set; }
8: private int orderId;
9:
10: public int OrderId
11: {
12: get { return orderId; }
13: set { orderId = value; }
14: }
15: private DateTime orderTime;
16:
17: [XmlIgnore]
18: public DateTime OrderTime
19: {
20: get { return orderTime; }
21: set { orderTime = value; }
22: }
23:
24: private List<Product> product;
25: public List<Product> Product
26: {
27: get { return product; }
28: set { product = value; }
29: }
30: }
31: public class Product
32: {
33: private int productId;
34:
35: public int ProductId
36: {
37: get { return productId; }
38: set { productId = value; }
39: }
40: private string productName;
41:
42: public string ProductName
43: {
44: get { return productName; }
45: set { productName = value; }
46: }
47:
48:
49: private string productShortName;
50:
51: public string ProductShortName
52: {
53: get { return productShortName; }
54: set { productShortName = value; }
55: }
56: }
再次查看结果
<?xml version="1.0" encoding="UTF-8"?>
-<Order xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><c>3</c><OrderId>1001</OrderId>-<Product>-<Product><ProductId>1</ProductId><ProductName>ipone4</ProductName><ProductShortName>ip4</ProductShortName></Product></Product></Order>
对比结果可以看出
1:可以使用一些attribute来改变制定property序列化之后的形式。
2:private\protect字段不会序列化,也可以说可以被外部读写的字段属性会被序列化。
3:需要使用默认的构造函数,原理类似第二条。
4:XmlIgnore attribute之后不会序列化。
二进制序列化
二进制序列化使用的是namespace:System.Runtime.Serialization.Formatters下的class:Binary实现的
同样使用类似的代码
1: [Serializable]
2: public class Order
3: {
4: private int a = 1;
5: protected int b = 2;
6: public int c = 3;
7: protected int d { get; set; }
8: private int orderId;
9:
10: public int OrderId
11: {
12: get { return orderId; }
13: set { orderId = value; }
14: }
15: private DateTime orderTime;
16:
17: public DateTime OrderTime
18: {
19: get { return orderTime; }
20: set { orderTime = value; }
21: }
22:
23: private List<Product> product;
24: public List<Product> Product
25: {
26: get { return product; }
27: set { product = value; }
28: }
29: }
30: [Serializable]
31: public class Product
32: {
33: private int productId;
34:
35:
36: public int ProductId
37: {
38: get { return productId; }
39: set { productId = value; }
40: }
41: private string productName;
42:
43: public string ProductName
44: {
45: get { return productName; }
46: set { productName = value; }
47: }
48:
49:
50: private string productShortName;
51:
52: public string ProductShortName
53: {
54: get { return productShortName; }
55: set { productShortName = value; }
56: }
序列化和反序列化:
1: Order order = new Order
2: {
3: Product = new List<Product>() { new Product() { ProductId = 1, ProductName = "ipone4", ProductShortName = "ip4" } },
4: OrderId = 1001,
5: OrderTime = DateTime.Now
6: };
7:
8: using (System.IO.FileStream fs = new System.IO.FileStream("xx.xml", System.IO.FileMode.OpenOrCreate))
9: {
10: BinaryFormatter bf = new BinaryFormatter();
11: bf.Serialize(fs, order);
12: }
13: using (System.IO.FileStream fs = new System.IO.FileStream("xx.xml", System.IO.FileMode.OpenOrCreate))
14: {
15: BinaryFormatter bf = new BinaryFormatter();
16: Order order1 = (Order)bf.Deserialize(fs);
17: }
查看结果:
看来所有的属性和字段都被序列化了。
看来,二进制序列化和XML序列化还是稍显不同的。
------未完待续-----