.NET 三种 序列化方式

1。 XML Serializer。这个是 ASP。NET 中 Web Service SOAP 请求的发送和接受默认使用的方式。指序列化对象的公共属性和成员。

2。 SOAP Serializer . DotNet Remoting 使用的对象传送方式。这个时候传送的对象要求有 Serializable 标志。

3. BinarySerializer 。同2, 只不过是二进制格式。

代码示例:

考虑一个 Person 对象,会有一些属性比如fullname,唯一 ID,电话(最多三个。)

[Serializable]
public class Phone
{
private string _phoneNumber="";
private string _phoneType="";
public Phone(string phoneType,string phoneNumner)
{
_phoneNumber=phoneNumner;
_phoneType=phoneType;
}

public string PhoneDescp
{
get
{
return string.Format("{0}\t{1}",_phoneType,_phoneNumber);

}
}

public Phone()
{

}
}

[Serializable]
public class Person
{
public Person()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

//电话列表假设只能最多有三个
private Phone [] _allPhones=new Phone[3];

//全称
private string fullName="";

//唯一的标识
private System.Guid id=System.Guid.NewGuid();

public string FullName
{
get{return fullName;}
set{fullName=value;}
}

///

1<summary>   
2/// 标识   
3/// </summary>

public System.Guid ID
{
get
{
return id;
}

}

public Phone this[int phoneIndex]
{

get
{
System.Diagnostics.Debug.Assert(phoneIndex>=0 && phoneIndex<=2);
return _allPhones[phoneIndex];
}
set
{
System.Diagnostics.Debug.Assert(phoneIndex>=0 && phoneIndex<=2);
_allPhones[phoneIndex]=value;
}
}

}

如果用 XML Serializer

只能看到一下结果。只序列华了 fullname

---------------------------

---------------------------

1<person xmlns:xsd=" http://www.w3.org/2001/XMLSchema " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance ">
2<fullname>MontaqueHou</fullname>
3</person>

---------------------------
OK
---------------------------

注意 GUID 和 Phone 都没有序列化。长度为195 个字节。

而采用 SOAP 序列华则序列化了所有,2022 个字节。

---------------------------

---------------------------

 1<soap-env:envelope soap-env:encodingstyle=" http://schemas.xmlsoap.org/soap/encoding/ " xmlns:clr=" http://schemas.microsoft.com/soap/encoding/clr/1.0 " xmlns:soap-enc=" http://schemas.xmlsoap.org/soap/encoding/ " xmlns:soap-env=" http://schemas.xmlsoap.org/soap/envelope/ " xmlns:xsd=" http://www.w3.org/2001/XMLSchema " xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance ">
 2<soap-env:body>
 3<a1:person id="ref-1" xmlns:a1=" http://schemas.microsoft.com/clr/nsassem/Comparation/Comparation%2C%20Version%3D1.0.1698.18683%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull ">
 4
 5&lt;_allPhones href="#ref-3"/&gt;
 6
 7<fullname id="ref-4">MontaqueHou</fullname>
 8<id>
 9
10&lt;_a&gt;120451046<!--_a-->
11
12&lt;_b&gt;-8025<!--_b-->
13
14&lt;_c&gt;17783<!--_c-->
15
16&lt;_d&gt;155<!--_d-->
17
18&lt;_e&gt;187<!--_e-->
19
20&lt;_f&gt;62<!--_f-->
21
22&lt;_g&gt;53<!--_g-->
23
24&lt;_h&gt;99<!--_h-->
25
26&lt;_i&gt;190<!--_i-->
27
28&lt;_j&gt;9<!--_j-->
29
30&lt;_k&gt;169<!--_k-->
31</id>
32</a1:person>
33<soap-enc:array id="ref-3" soap-enc:arraytype="a1:Phone[3]" xmlns:a1=" http://schemas.microsoft.com/clr/nsassem/Comparation/Comparation%2C%20Version%3D1.0.1698.18683%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull ">
34<item href="#ref-5"></item>
35<item href="#ref-6"></item>
36<item href="#ref-7"></item>
37</soap-enc:array>
38<a1:phone id="ref-5" xmlns:a1=" http://schemas.microsoft.com/clr/nsassem/Comparation/Comparation%2C%20Version%3D1.0.1698.18683%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull ">
39
40&lt;_phoneNumber id="ref-8"&gt;13817327101-000<!--_phoneNumber-->
41
42&lt;_phoneType id="ref-9"&gt;Type 1<!--_phoneType-->
43</a1:phone>
44<a1:phone id="ref-6" xmlns:a1=" http://schemas.microsoft.com/clr/nsassem/Comparation/Comparation%2C%20Version%3D1.0.1698.18683%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull ">
45
46&lt;_phoneNumber id="ref-10"&gt;13817327101-001<!--_phoneNumber-->
47
48&lt;_phoneType href="#ref-9"/&gt;
49
50</a1:phone>
51<a1:phone id="ref-7" xmlns:a1=" http://schemas.microsoft.com/clr/nsassem/Comparation/Comparation%2C%20Version%3D1.0.1698.18683%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull ">
52
53&lt;_phoneNumber id="ref-11"&gt;13817327101-002<!--_phoneNumber-->
54
55&lt;_phoneType href="#ref-9"/&gt;
56
57</a1:phone>
58</soap-env:body>
59</soap-env:envelope>

---------------------------
OK
---------------------------

Binary 也序列华了所有属性。517 个字节

---------------------------

---------------------------
00-01-00-00-00-FF-FF-FF-FF-01-00-00-00-00-00-00-00-0C-02-00-00-00-49-43-6F-6D-70-61-72-61-74-69-6F-6E-2C-20-56-65-72-73-69-6F-6E-3D-31-2E-30-2E-31-36-39-38-2E-31-38-36-38-33-2C-20-43-75-6C-74-75-72-65-3D-6E-65-75-74-72-61-6C-2C-20-50-75-62-6C-69-63-4B-65-79-54-6F-6B-65-6E-3D-6E-75-6C-6C-05-01-00-00-00-12-43-6F-6D-70-61-72-61-74-69-6F-6E-2E-50-65-72-73-6F-6E-03-00-00-00-0A-5F-61-6C-6C-50-68-6F-6E-65-73-08-66-75-6C-6C-4E-61-6D-65-02-69-64-04-01-03-13-43-6F-6D-70-61-72-61-74-69-6F-6E-2E-50-68-6F-6E-65-5B-5D-02-00-00-00-0B-53-79-73-74-65-6D-2E-47-75-69-64-02-00-00-00-09-03-00-00-00-06-04-00-00-00-0B-4D-6F-6E-74-61-71-75-65-48-6F-75-04-FB-FF-FF-FF-0B-53-79-73-74-65-6D-2E-47-75-69-64-0B-00-00-00-02-5F-61-02-5F-62-02-5F-63-02-5F-64-02-5F-65-02-5F-66-02-5F-67-02-5F-68-02-5F-69-02-5F-6A-02-5F-6B-00-00-00-00-00-00-00-00-00-00-00-08-07-07-02-02-02-02-02-02-02-02-E6-EF-2D-07-A7-E0-77-45-9B-BB-3E-35-63-BE-09-A9-07-03-00-00-00-00-01-00-00-00-03-00-00-00-04-11-43-6F-6D-70-61-72-61-74-69-6F-6E-2E-50-68-6F-6E-65-02-00-00-00-09-06-00-00-00-09-07-00-00-00-09-08-00-00-00-05-06-00-00-00-11-43-6F-6D-70-61-72-61-74-69-6F-6E-2E-50-68-6F-6E-65-02-00-00-00-0C-5F-70-68-6F-6E-65-4E-75-6D-62-65-72-0A-5F-70-68-6F-6E-65-54-79-70-65-01-01-02-00-00-00-06-09-00-00-00-0F-31-33-38-31-37-33-32-37-31-30-31-2D-30-30-30-06-0A-00-00-00-06-54-79-70-65-20-31-01-07-00-00-00-06-00-00-00-06-0B-00-00-00-0F-31-33-38-31-37-33-32-37-31-30-31-2D-30-30-31-09-0A-00-00-00-01-08-00-00-00-06-00-00-00-06-0D-00-00-00-0F-31-33-38-31-37-33-32-37-31-30-31-2D-30-30-32-09-0A-00-00-00-0B
---------------------------
OK
---------------------------

看了这个结果你就明白DotNEt remoting 的优越性了。hh

Published At
Categories with Web编程
Tagged with
comments powered by Disqus