首页 > 开发 > .Net > 正文

.NET 三种 序列化方式

2020-02-03 16:00:34
字体:
来源:转载
供稿:网友
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

国内最大的酷站演示中心!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表