http://www.itstrike.cn/Question/WCF-Serializer
介绍
WCF(Windows Communication Foundation) - 序列化:本文分别以DataContractSerializer,XmlSerializer,DataContractJsonSerializer,SoapFormatter,BinaryFormatter为例。
示例
1、服务
DataContractSerializerObject.cs
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Runtime.Serialization;
namespace WCF.ServiceLib.Serialization
{
///<summary>
@H_301_56@用于演示DataContract序列化的实体类
</summary>
//Namespace-数据契约的命名空间
[DataContract(Namespace="@H_301_56@http://webabcd.cnblogs.com/@H_301_56@")]
publicclassDataContractSerializerObject
{
Name-数据成员的名称
Order-数据成员的序列化和反序列化的顺序[DataMember(NameUniqueID@H_301_56@,Order0GuidID{get;set;}
[DataMember(Order1stringName2intAge3DateTimeTime
}
}
System.Collections.Generic;
System.Linq;
System.Text;
System.Runtime.Serialization;
namespace WCF.ServiceLib.Serialization
{
///<summary>
@H_301_56@用于演示DataContract序列化的实体类
</summary>
//Namespace-数据契约的命名空间
[DataContract(Namespace="@H_301_56@http://webabcd.cnblogs.com/@H_301_56@")]
publicclassDataContractSerializerObject
{
Name-数据成员的名称
Order-数据成员的序列化和反序列化的顺序[DataMember(NameUniqueID@H_301_56@,Order0GuidID{get;set;}
[DataMember(Order1stringName2intAge3DateTimeTime
}
}
XmlSerializerObject.cs
System.Runtime.Serialization;
System.Xml.Serialization;
WCF.ServiceLib.Serialization
{
@H_301_56@用于演示XML序列化的实体类
@H_301_56@Namespace-XML根元素的命名空间[XmlRoot(NamespaceXmlSerializerObject
ElementName-生成的XML元素的名称
Order-序列化和反序列化的顺序[XmlElement(ElementNameGuidID
[XmlElement(OrderNameAgeDateTimeTimeSoapFormatterObject.cs
WCF.ServiceLib.Serialization
{
@H_301_56@用于演示Soap序列化的实体类
@H_301_56@[Serializable]
SoapFormatterOjbect
GuidIDNameAgeDateTimeTimeBinaryFormatterObject.cs
WCF.ServiceLib.Serialization
{
@H_301_56@用于演示Binary序列化的实体类
@H_301_56@BinaryFormatterObject
GuidIDNameAgeDateTimeTimeDataContractJsonSerializerObject.cs
WCF.ServiceLib.Serialization
{
@H_301_56@用于演示JSON序列化的实体类
@H_301_56@DataContractJsonSerializerObject
GuidIDNameAgeDateTimeTime@H_492_301@
2、演示
Sample.aspx.cs
System.Collections;
System.Configuration;
System.Data;
System.Web;
System.Web.Security;
System.Web.UI;
System.Web.UI.HtmlControls;
System.Web.UI.WebControls;
System.Web.UI.WebControls.WebParts;
System.Xml.Linq;
System.IO;
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();
}
@H_301_56@演示DataContractSerializer的序列化和反序列化
@H_301_56@ShowDataContractSerializer()
{
vardataContractSerializerObjectnewAPI.DataContractSerializerObject{IDGuid.NewGuid(),NameDataContractSerializer28DateTime.Now};
varserializerSystem.Runtime.Serialization.DataContractSerializer(typeof(API.DataContractSerializerObject));
序列化varmsMemoryStream();
serializer.WriteObject(ms,dataContractSerializerObject);
ms.Position;
varsrStreamReader(ms);
varstrsr.ReadToEnd();
txtDataContractSerializer.Textstr;
反序列化varbufferSystem.Text.Encoding.UTF8.GetBytes(str);
varms2MemoryStream(buffer);
vardataContractSerializerObject2serializer.ReadObject(ms2)asAPI.DataContractSerializerObject;
lblDataContractSerializer.TextdataContractSerializerObject2.Name;
}
@H_301_56@演示XmlSerializer的序列化和反序列化
@H_301_56@ShowXmlSerializer()
{
varxmlSerializerObjectAPI.XmlSerializerObjectXmlSerializer@H_301_56@System.Xml.Serialization.XmlSerializer((API.XmlSerializerObject));
MemoryStream();
serializer.Serialize(ms,xmlSerializerObject);
ms.Positionsr.ReadToEnd();
txtXmlSerializer.TextMemoryStream(buffer);
varxmlSerializerObject2serializer.Deserialize(ms2)API.XmlSerializerObject;
lblXmlSerializer.TextxmlSerializerObject2.Name;
}
@H_301_56@演示SoapFormatter的序列化和反序列化
@H_301_56@ShowSoapFormatter()
{
varsoapFormatterOjbectAPI.SoapFormatterOjbectShowSoapFormatter@H_301_56@;
varformatterSystem.Runtime.Serialization.Formatters.Soap.SoapFormatter();
MemoryStream();
formatter.Serialize(ms,soapFormatterOjbect);
ms.Position;
varstrSystem.Text.Encoding.UTF8.GetString(ms.GetBuffer());
txtSoapFormatter.TextMemoryStream(buffer);
varsoapFormatterOjbect2formatter.Deserialize(ms2)API.SoapFormatterOjbect;
lblSoapFormatter.TextsoapFormatterOjbect2.Name;
}
@H_301_56@演示BinaryFormatter的序列化和反序列化
@H_301_56@ShowBinaryFormatter()
{
varbinaryFormatterObjectAPI.BinaryFormatterObjectBinaryFormatter@H_301_56@System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
;
varbufferms.GetBuffer();
varstrSystem.Text.Encoding.UTF8.GetString(buffer);
txtBinaryFormatter.Textvarms2MemoryStream(buffer);
varbinaryFormatterObject2API.BinaryFormatterObject;
lblBinaryFormatter.TextbinaryFormatterObject2.Name;
}
@H_301_56@演示DataContractJsonSerializer的序列化和反序列化
@H_301_56@ShowDataContractJsonSerializer()
{
vardataContractJsonSerializerObjectAPI.DataContractJsonSerializerObjectDataContractJsonSerializer@H_301_56@System.Runtime.Serialization.Json.DataContractJsonSerializer((API.DataContractJsonSerializerObject));
sr.ReadToEnd();
txtDataContractJsonSerializer.TextMemoryStream(buffer);
vardataContractJsonSerializerObject2API.DataContractJsonSerializerObject;
lblDataContractJsonSerializer.TextdataContractJsonSerializerObject2.Name;
} }
System.Xml.Serialization;
WCF.ServiceLib.Serialization
{
@H_301_56@用于演示XML序列化的实体类
@H_301_56@Namespace-XML根元素的命名空间[XmlRoot(NamespaceXmlSerializerObject
ElementName-生成的XML元素的名称
Order-序列化和反序列化的顺序[XmlElement(ElementNameGuidID
[XmlElement(OrderNameAgeDateTimeTimeSoapFormatterObject.cs
WCF.ServiceLib.Serialization
{
@H_301_56@用于演示Soap序列化的实体类
@H_301_56@[Serializable]
SoapFormatterOjbect
GuidIDNameAgeDateTimeTimeBinaryFormatterObject.cs
WCF.ServiceLib.Serialization
{
@H_301_56@用于演示Binary序列化的实体类
@H_301_56@BinaryFormatterObject
GuidIDNameAgeDateTimeTimeDataContractJsonSerializerObject.cs
WCF.ServiceLib.Serialization
{
@H_301_56@用于演示JSON序列化的实体类
@H_301_56@DataContractJsonSerializerObject
GuidIDNameAgeDateTimeTime@H_492_301@
2、演示
Sample.aspx.cs
System.Collections;
System.Configuration;
System.Data;
System.Web;
System.Web.Security;
System.Web.UI;
System.Web.UI.HtmlControls;
System.Web.UI.WebControls;
System.Web.UI.WebControls.WebParts;
System.Xml.Linq;
System.IO;
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();
}
@H_301_56@演示DataContractSerializer的序列化和反序列化
@H_301_56@ShowDataContractSerializer()
{
vardataContractSerializerObjectnewAPI.DataContractSerializerObject{IDGuid.NewGuid(),NameDataContractSerializer28DateTime.Now};
varserializerSystem.Runtime.Serialization.DataContractSerializer(typeof(API.DataContractSerializerObject));
序列化varmsMemoryStream();
serializer.WriteObject(ms,dataContractSerializerObject);
ms.Position;
varsrStreamReader(ms);
varstrsr.ReadToEnd();
txtDataContractSerializer.Textstr;
反序列化varbufferSystem.Text.Encoding.UTF8.GetBytes(str);
varms2MemoryStream(buffer);
vardataContractSerializerObject2serializer.ReadObject(ms2)asAPI.DataContractSerializerObject;
lblDataContractSerializer.TextdataContractSerializerObject2.Name;
}
@H_301_56@演示XmlSerializer的序列化和反序列化
@H_301_56@ShowXmlSerializer()
{
varxmlSerializerObjectAPI.XmlSerializerObjectXmlSerializer@H_301_56@System.Xml.Serialization.XmlSerializer((API.XmlSerializerObject));
MemoryStream();
serializer.Serialize(ms,xmlSerializerObject);
ms.Positionsr.ReadToEnd();
txtXmlSerializer.TextMemoryStream(buffer);
varxmlSerializerObject2serializer.Deserialize(ms2)API.XmlSerializerObject;
lblXmlSerializer.TextxmlSerializerObject2.Name;
}
@H_301_56@演示SoapFormatter的序列化和反序列化
@H_301_56@ShowSoapFormatter()
{
varsoapFormatterOjbectAPI.SoapFormatterOjbectShowSoapFormatter@H_301_56@;
varformatterSystem.Runtime.Serialization.Formatters.Soap.SoapFormatter();
MemoryStream();
formatter.Serialize(ms,soapFormatterOjbect);
ms.Position;
varstrSystem.Text.Encoding.UTF8.GetString(ms.GetBuffer());
txtSoapFormatter.TextMemoryStream(buffer);
varsoapFormatterOjbect2formatter.Deserialize(ms2)API.SoapFormatterOjbect;
lblSoapFormatter.TextsoapFormatterOjbect2.Name;
}
@H_301_56@演示BinaryFormatter的序列化和反序列化
@H_301_56@ShowBinaryFormatter()
{
varbinaryFormatterObjectAPI.BinaryFormatterObjectBinaryFormatter@H_301_56@System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
;
varbufferms.GetBuffer();
varstrSystem.Text.Encoding.UTF8.GetString(buffer);
txtBinaryFormatter.Textvarms2MemoryStream(buffer);
varbinaryFormatterObject2API.BinaryFormatterObject;
lblBinaryFormatter.TextbinaryFormatterObject2.Name;
}
@H_301_56@演示DataContractJsonSerializer的序列化和反序列化
@H_301_56@ShowDataContractJsonSerializer()
{
vardataContractJsonSerializerObjectAPI.DataContractJsonSerializerObjectDataContractJsonSerializer@H_301_56@System.Runtime.Serialization.Json.DataContractJsonSerializer((API.DataContractJsonSerializerObject));
sr.ReadToEnd();
txtDataContractJsonSerializer.TextMemoryStream(buffer);
vardataContractJsonSerializerObject2API.DataContractJsonSerializerObject;
lblDataContractJsonSerializer.TextdataContractJsonSerializerObject2.Name;
} }