.NET 三种 序列化方式

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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;}

}

/// <summary>

/// 标识

/// </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

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

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

<?xml version="1.0" encoding="utf-16"?>

<Person xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<FullName>MontaqueHou</FullName>

</Person>

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

OK

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

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

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

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

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

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">

<SOAP-ENV:Body>

<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">

<_allPhones href="#ref-3"/>

<fullName id="ref-4">MontaqueHou</fullName>

<id>

<_a>120451046</_a>

<_b>-8025</_b>

<_c>17783</_c>

<_d>155</_d>

<_e>187</_e>

<_f>62</_f>

<_g>53</_g>

<_h>99</_h>

<_i>190</_i>

<_j>9</_j>

<_k>169</_k>

</id>

</a1:Person>

<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">

<item href="#ref-5"/>

<item href="#ref-6"/>

<item href="#ref-7"/>

</SOAP-ENC:Array>

<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">

<_phoneNumber id="ref-8">13817327101-000</_phoneNumber>

<_phoneType id="ref-9">Type 1</_phoneType>

</a1:Phone>

<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">

<_phoneNumber id="ref-10">13817327101-001</_phoneNumber>

<_phoneType href="#ref-9"/>

</a1:Phone>

<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">

<_phoneNumber id="ref-11">13817327101-002</_phoneNumber>

<_phoneType href="#ref-9"/>

</a1:Phone>

</SOAP-ENV:Body>

</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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航