[源码下载]
化零为整WCF(9) - 序列化(DataContractSerializer,XmlSerializer,DataContractJsonSerializer,SoapFormatter,BinaryFormatter)
作者: webabcd
介绍
WCF(Windows Communication Foundation) - 序列化:本文分别以DataContractSerializer,BinaryFormatter为例。
示例
1、服务
DataContractSerializerObject.cs
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace WCF.ServiceLib.Serialization
{
///<summary>
///用于演示DataContract序列化的实体类
///</summary>
//Namespace-数据契约的命名空间
[DataContract(Namespace="http://webabcd.cnblogs.com/")]
publicclassDataContractSerializerObject
{
//Name-数据成员的名称
//Order-数据成员的序列化和反序列化的顺序
[DataMember(Name="UniqueID",Order=0)]
publicGuidID{get;set;}
[DataMember(Order=1)]
publicstringName{get;set;}
[DataMember(Order=2)]
publicintAge{get;set;}
[DataMember(Order=3)]
publicDateTimeTime{get;set;}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace WCF.ServiceLib.Serialization
{
///<summary>
///用于演示DataContract序列化的实体类
///</summary>
//Namespace-数据契约的命名空间
[DataContract(Namespace="http://webabcd.cnblogs.com/")]
publicclassDataContractSerializerObject
{
//Name-数据成员的名称
//Order-数据成员的序列化和反序列化的顺序
[DataMember(Name="UniqueID",Order=0)]
publicGuidID{get;set;}
[DataMember(Order=1)]
publicstringName{get;set;}
[DataMember(Order=2)]
publicintAge{get;set;}
[DataMember(Order=3)]
publicDateTimeTime{get;set;}
}
}
XmlSerializerObject.cs
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace WCF.ServiceLib.Serialization
{
///<summary>
///用于演示XML序列化的实体类
///</summary>
//Namespace-XML根元素的命名空间
[XmlRoot(Namespace="http://webabcd.cnblogs.com/")]
publicclassXmlSerializerObject
{
//ElementName-生成的XML元素的名称
//Order-序列化和反序列化的顺序
[XmlElement(ElementName="UniqueID",Order=0)]
publicGuidID{get;set;}
[XmlElement(Order=1)]
publicstringName{get;set;}
[XmlElement(Order=2)]
publicintAge{get;set;}
[XmlElement(Order=3)]
publicDateTimeTime{get;set;}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace WCF.ServiceLib.Serialization
{
///<summary>
///用于演示XML序列化的实体类
///</summary>
//Namespace-XML根元素的命名空间
[XmlRoot(Namespace="http://webabcd.cnblogs.com/")]
publicclassXmlSerializerObject
{
//ElementName-生成的XML元素的名称
//Order-序列化和反序列化的顺序
[XmlElement(ElementName="UniqueID",Order=0)]
publicGuidID{get;set;}
[XmlElement(Order=1)]
publicstringName{get;set;}
[XmlElement(Order=2)]
publicintAge{get;set;}
[XmlElement(Order=3)]
publicDateTimeTime{get;set;}
}
}
SoapFormatterObject.cs
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace WCF.ServiceLib.Serialization
{
///<summary>
///用于演示Soap序列化的实体类
///</summary>
[Serializable]
publicclassSoapFormatterOjbect
{
publicGuidID{get;set;}
publicstringName{get;set;}
publicintAge{get;set;}
publicDateTimeTime{get;set;}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace WCF.ServiceLib.Serialization
{
///<summary>
///用于演示Soap序列化的实体类
///</summary>
[Serializable]
publicclassSoapFormatterOjbect
{
publicGuidID{get;set;}
publicstringName{get;set;}
publicintAge{get;set;}
publicDateTimeTime{get;set;}
}
}
BinaryFormatterObject.cs
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace WCF.ServiceLib.Serialization
{
///<summary>
///用于演示Binary序列化的实体类
///</summary>
[Serializable]
publicclassBinaryFormatterObject
{
publicGuidID{get;set;}
publicstringName{get;set;}
publicintAge{get;set;}
publicDateTimeTime{get;set;}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;
namespace WCF.ServiceLib.Serialization
{
///<summary>
///用于演示Binary序列化的实体类
///</summary>
[Serializable]
publicclassBinaryFormatterObject
{
publicGuidID{get;set;}
publicstringName{get;set;}
publicintAge{get;set;}
publicDateTimeTime{get;set;}
}
}
DataContractJsonSerializerObject.cs
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace WCF.ServiceLib.Serialization
{
///<summary>
///用于演示JSON序列化的实体类
///</summary>
//Namespace-数据契约的命名空间
[DataContract(Namespace="http://webabcd.cnblogs.com/")]
publicclassDataContractJsonSerializerObject
{
//Name-数据成员的名称
//Order-数据成员的序列化和反序列化的顺序
[DataMember(Name="UniqueID",Order=0)]
publicGuidID{get;set;}
[DataMember(Order=1)]
publicstringName{get;set;}
[DataMember(Order=2)]
publicintAge{get;set;}
[DataMember(Order=3)]
publicDateTimeTime{get;set;}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
namespace WCF.ServiceLib.Serialization
{
///<summary>
///用于演示JSON序列化的实体类
///</summary>
//Namespace-数据契约的命名空间
[DataContract(Namespace="http://webabcd.cnblogs.com/")]
publicclassDataContractJsonSerializerObject
{
//Name-数据成员的名称
//Order-数据成员的序列化和反序列化的顺序
[DataMember(Name="UniqueID",Order=0)]
publicGuidID{get;set;}
[DataMember(Order=1)]
publicstringName{get;set;}
[DataMember(Order=2)]
publicintAge{get;set;}
[DataMember(Order=3)]
publicDateTimeTime{get;set;}
}
}
2、演示
Sample.aspx.cs
using
System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using API = WCF.ServiceLib.Serialization;
public partial class Serialization_Sample:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
//演示DataContractSerializer的序列化和反序列化
ShowDataContractSerializer();
//演示XmlSerializer的序列化和反序列化
ShowXmlSerializer();
//演示SoapFormatter的序列化和反序列化
ShowSoapFormatter();
//演示BinaryFormatter的序列化和反序列化
ShowBinaryFormatter();
//演示DataContractJsonSerializer的序列化和反序列化
ShowDataContractJsonSerializer();
}
///<summary>
///演示DataContractSerializer的序列化和反序列化
///</summary>
voidShowDataContractSerializer()
{
vardataContractSerializerObject=newAPI.DataContractSerializerObject{ID=Guid.NewGuid(),Name="DataContractSerializer",Age=28,Time=DateTime.Now};
varserializer=newSystem.Runtime.Serialization.DataContractSerializer(typeof(API.DataContractSerializerObject));
//序列化
varms=newMemoryStream();
serializer.WriteObject(ms,dataContractSerializerObject);
ms.Position=0;
varsr=newStreamReader(ms);
varstr=sr.ReadToEnd();
txtDataContractSerializer.Text=str;
//反序列化
varbuffer=System.Text.Encoding.UTF8.GetBytes(str);
varms2=newMemoryStream(buffer);
vardataContractSerializerObject2=serializer.ReadObject(ms2)asAPI.DataContractSerializerObject;
lblDataContractSerializer.Text=dataContractSerializerObject2.Name;
}
///<summary>
///演示XmlSerializer的序列化和反序列化
///</summary>
voidShowXmlSerializer()
{
varxmlSerializerObject=newAPI.XmlSerializerObject{ID=Guid.NewGuid(),Name="XmlSerializer",Time=DateTime.Now};
varserializer=newSystem.Xml.Serialization.XmlSerializer(typeof(API.XmlSerializerObject));
//序列化
varms=newMemoryStream();
serializer.Serialize(ms,xmlSerializerObject);
ms.Position=0;
varsr=newStreamReader(ms);
varstr=sr.ReadToEnd();
txtXmlSerializer.Text=str;
//反序列化
varbuffer=System.Text.Encoding.UTF8.GetBytes(str);
varms2=newMemoryStream(buffer);
varxmlSerializerObject2=serializer.Deserialize(ms2)asAPI.XmlSerializerObject;
lblXmlSerializer.Text=xmlSerializerObject2.Name;
}
///<summary>
///演示SoapFormatter的序列化和反序列化
///</summary>
voidShowSoapFormatter()
{
varsoapFormatterOjbect=newAPI.SoapFormatterOjbect{ID=Guid.NewGuid(),Name="ShowSoapFormatter",Time=DateTime.Now};
varformatter=newSystem.Runtime.Serialization.Formatters.Soap.SoapFormatter();
//序列化
varms=newMemoryStream();
formatter.Serialize(ms,soapFormatterOjbect);
ms.Position=0;
varstr=System.Text.Encoding.UTF8.GetString(ms.GetBuffer());
txtSoapFormatter.Text=str;
//反序列化
varbuffer=System.Text.Encoding.UTF8.GetBytes(str);
varms2=newMemoryStream(buffer);
varsoapFormatterOjbect2=formatter.Deserialize(ms2)asAPI.SoapFormatterOjbect;
lblSoapFormatter.Text=soapFormatterOjbect2.Name;
}
///<summary>
///演示BinaryFormatter的序列化和反序列化
///</summary>
voidShowBinaryFormatter()
{
varbinaryFormatterObject=newAPI.BinaryFormatterObject{ID=Guid.NewGuid(),Name="BinaryFormatter",Time=DateTime.Now};
varformatter=newSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
//序列化
varms=newMemoryStream();
formatter.Serialize(ms,binaryFormatterObject);
ms.Position=0;
varbuffer=ms.GetBuffer();
varstr=System.Text.Encoding.UTF8.GetString(buffer);
txtBinaryFormatter.Text=str;
//反序列化
varms2=newMemoryStream(buffer);
varbinaryFormatterObject2=formatter.Deserialize(ms2)asAPI.BinaryFormatterObject;
lblBinaryFormatter.Text=binaryFormatterObject2.Name;
}
///<summary>
///演示DataContractJsonSerializer的序列化和反序列化
///</summary>
voidShowDataContractJsonSerializer()
{
vardataContractJsonSerializerObject=newAPI.DataContractJsonSerializerObject{ID=Guid.NewGuid(),Name="DataContractJsonSerializer",Time=DateTime.Now};
varserializer=newSystem.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(API.DataContractJsonSerializerObject));
//序列化
varms=newMemoryStream();
serializer.WriteObject(ms,dataContractJsonSerializerObject);
ms.Position=0;
varsr=newStreamReader(ms);
varstr=sr.ReadToEnd();
txtDataContractJsonSerializer.Text=str;
//反序列化
varbuffer=System.Text.Encoding.UTF8.GetBytes(str);
varms2=newMemoryStream(buffer);
vardataContractJsonSerializerObject2=serializer.ReadObject(ms2)asAPI.DataContractJsonSerializerObject;
lblDataContractJsonSerializer.Text=dataContractJsonSerializerObject2.Name;
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using API = WCF.ServiceLib.Serialization;
public partial class Serialization_Sample:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
//演示DataContractSerializer的序列化和反序列化
ShowDataContractSerializer();
//演示XmlSerializer的序列化和反序列化
ShowXmlSerializer();
//演示SoapFormatter的序列化和反序列化
ShowSoapFormatter();
//演示BinaryFormatter的序列化和反序列化
ShowBinaryFormatter();
//演示DataContractJsonSerializer的序列化和反序列化
ShowDataContractJsonSerializer();
}
///<summary>
///演示DataContractSerializer的序列化和反序列化
///</summary>
voidShowDataContractSerializer()
{
vardataContractSerializerObject=newAPI.DataContractSerializerObject{ID=Guid.NewGuid(),Name="DataContractSerializer",Age=28,Time=DateTime.Now};
varserializer=newSystem.Runtime.Serialization.DataContractSerializer(typeof(API.DataContractSerializerObject));
//序列化
varms=newMemoryStream();
serializer.WriteObject(ms,dataContractSerializerObject);
ms.Position=0;
varsr=newStreamReader(ms);
varstr=sr.ReadToEnd();
txtDataContractSerializer.Text=str;
//反序列化
varbuffer=System.Text.Encoding.UTF8.GetBytes(str);
varms2=newMemoryStream(buffer);
vardataContractSerializerObject2=serializer.ReadObject(ms2)asAPI.DataContractSerializerObject;
lblDataContractSerializer.Text=dataContractSerializerObject2.Name;
}
///<summary>
///演示XmlSerializer的序列化和反序列化
///</summary>
voidShowXmlSerializer()
{
varxmlSerializerObject=newAPI.XmlSerializerObject{ID=Guid.NewGuid(),Name="XmlSerializer",Time=DateTime.Now};
varserializer=newSystem.Xml.Serialization.XmlSerializer(typeof(API.XmlSerializerObject));
//序列化
varms=newMemoryStream();
serializer.Serialize(ms,xmlSerializerObject);
ms.Position=0;
varsr=newStreamReader(ms);
varstr=sr.ReadToEnd();
txtXmlSerializer.Text=str;
//反序列化
varbuffer=System.Text.Encoding.UTF8.GetBytes(str);
varms2=newMemoryStream(buffer);
varxmlSerializerObject2=serializer.Deserialize(ms2)asAPI.XmlSerializerObject;
lblXmlSerializer.Text=xmlSerializerObject2.Name;
}
///<summary>
///演示SoapFormatter的序列化和反序列化
///</summary>
voidShowSoapFormatter()
{
varsoapFormatterOjbect=newAPI.SoapFormatterOjbect{ID=Guid.NewGuid(),Name="ShowSoapFormatter",Time=DateTime.Now};
varformatter=newSystem.Runtime.Serialization.Formatters.Soap.SoapFormatter();
//序列化
varms=newMemoryStream();
formatter.Serialize(ms,soapFormatterOjbect);
ms.Position=0;
varstr=System.Text.Encoding.UTF8.GetString(ms.GetBuffer());
txtSoapFormatter.Text=str;
//反序列化
varbuffer=System.Text.Encoding.UTF8.GetBytes(str);
varms2=newMemoryStream(buffer);
varsoapFormatterOjbect2=formatter.Deserialize(ms2)asAPI.SoapFormatterOjbect;
lblSoapFormatter.Text=soapFormatterOjbect2.Name;
}
///<summary>
///演示BinaryFormatter的序列化和反序列化
///</summary>
voidShowBinaryFormatter()
{
varbinaryFormatterObject=newAPI.BinaryFormatterObject{ID=Guid.NewGuid(),Name="BinaryFormatter",Time=DateTime.Now};
varformatter=newSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
//序列化
varms=newMemoryStream();
formatter.Serialize(ms,binaryFormatterObject);
ms.Position=0;
varbuffer=ms.GetBuffer();
varstr=System.Text.Encoding.UTF8.GetString(buffer);
txtBinaryFormatter.Text=str;
//反序列化
varms2=newMemoryStream(buffer);
varbinaryFormatterObject2=formatter.Deserialize(ms2)asAPI.BinaryFormatterObject;
lblBinaryFormatter.Text=binaryFormatterObject2.Name;
}
///<summary>
///演示DataContractJsonSerializer的序列化和反序列化
///</summary>
voidShowDataContractJsonSerializer()
{
vardataContractJsonSerializerObject=newAPI.DataContractJsonSerializerObject{ID=Guid.NewGuid(),Name="DataContractJsonSerializer",Time=DateTime.Now};
varserializer=newSystem.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(API.DataContractJsonSerializerObject));
//序列化
varms=newMemoryStream();
serializer.WriteObject(ms,dataContractJsonSerializerObject);
ms.Position=0;
varsr=newStreamReader(ms);
varstr=sr.ReadToEnd();
txtDataContractJsonSerializer.Text=str;
//反序列化
varbuffer=System.Text.Encoding.UTF8.GetBytes(str);
varms2=newMemoryStream(buffer);
vardataContractJsonSerializerObject2=serializer.ReadObject(ms2)asAPI.DataContractJsonSerializerObject;
lblDataContractJsonSerializer.Text=dataContractJsonSerializerObject2.Name;
}
}
运行结果:
DataContractSerializer
XmlSerializer
ShowSoapFormatter
BinaryFormatter
DataContractJsonSerializer
OK
[源码下载]