WCF(10):序列化(DataContractSerializer, XmlSerializer, DataContractJsonSerializer, SoapFormatter, Binary

前端之家收集整理的这篇文章主要介绍了WCF(10):序列化(DataContractSerializer, XmlSerializer, DataContractJsonSerializer, SoapFormatter, Binary前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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@,Order
0GuidID{get;set;}

[DataMember(Order
1stringName2intAge3DateTimeTime
}

}


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(Order
NameAgeDateTimeTimeSoapFormatterObject.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()
{
vardataContractSerializerObject
newAPI.DataContractSerializerObject{IDGuid.NewGuid(),NameDataContractSerializer28DateTime.Now};

varserializer
System.Runtime.Serialization.DataContractSerializer(typeof(API.DataContractSerializerObject));

序列化varmsMemoryStream();

serializer.WriteObject(ms,dataContractSerializerObject);

ms.Position
;
varsr
StreamReader(ms);
varstr
sr.ReadToEnd();
txtDataContractSerializer.Text
str;


反序列化varbufferSystem.Text.Encoding.UTF8.GetBytes(str);
varms2
MemoryStream(buffer);
vardataContractSerializerObject2
serializer.ReadObject(ms2)asAPI.DataContractSerializerObject;
lblDataContractSerializer.Text
dataContractSerializerObject2.Name;
}


@H_301_56@演示XmlSerializer的序列化和反序列化
@H_301_56@ShowXmlSerializer()
{
varxmlSerializerObject
API.XmlSerializerObjectXmlSerializer@H_301_56@System.Xml.Serialization.XmlSerializer((API.XmlSerializerObject));

MemoryStream();

serializer.Serialize(ms,xmlSerializerObject);

ms.Position
sr.ReadToEnd();
txtXmlSerializer.Text
MemoryStream(buffer);
varxmlSerializerObject2
serializer.Deserialize(ms2)API.XmlSerializerObject;
lblXmlSerializer.Text
xmlSerializerObject2.Name;
}


@H_301_56@演示SoapFormatter的序列化和反序列化
@H_301_56@ShowSoapFormatter()
{
varsoapFormatterOjbect
API.SoapFormatterOjbectShowSoapFormatter@H_301_56@;

varformatter
System.Runtime.Serialization.Formatters.Soap.SoapFormatter();

MemoryStream();

formatter.Serialize(ms,soapFormatterOjbect);

ms.Position
;
varstr
System.Text.Encoding.UTF8.GetString(ms.GetBuffer());
txtSoapFormatter.Text
MemoryStream(buffer);
varsoapFormatterOjbect2
formatter.Deserialize(ms2)API.SoapFormatterOjbect;
lblSoapFormatter.Text
soapFormatterOjbect2.Name;
}


@H_301_56@演示BinaryFormatter的序列化和反序列化
@H_301_56@ShowBinaryFormatter()
{
varbinaryFormatterObject
API.BinaryFormatterObjectBinaryFormatter@H_301_56@System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

;
varbuffer
ms.GetBuffer();
varstr
System.Text.Encoding.UTF8.GetString(buffer);
txtBinaryFormatter.Text
varms2MemoryStream(buffer);
varbinaryFormatterObject2
API.BinaryFormatterObject;
lblBinaryFormatter.Text
binaryFormatterObject2.Name;
}


@H_301_56@演示DataContractJsonSerializer的序列化和反序列化
@H_301_56@ShowDataContractJsonSerializer()
{
vardataContractJsonSerializerObject
API.DataContractJsonSerializerObjectDataContractJsonSerializer@H_301_56@System.Runtime.Serialization.Json.DataContractJsonSerializer((API.DataContractJsonSerializerObject));

sr.ReadToEnd();
txtDataContractJsonSerializer.Text
MemoryStream(buffer);
vardataContractJsonSerializerObject2
API.DataContractJsonSerializerObject;
lblDataContractJsonSerializer.Text
dataContractJsonSerializerObject2.Name;
}
}

猜你在找的Json相关文章