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>
用于演示DataContract序列化的实体类
</summary>

//Namespace-数据契约的命名空间
[DataContract(Namespace="http://webabcd.cnblogs.com/")]
publicclassDataContractSerializerObject
{
Name-数据成员的名称
Order-数据成员的序列化和反序列化的顺序[DataMember(NameUniqueID,Order0GuidID{get;set;}

[DataMember(Order
1stringName2intAge3DateTimeTime
}

}


XmlSerializerObject.cs
System.Runtime.Serialization;
System.Xml.Serialization;

WCF.ServiceLib.Serialization
{
用于演示XML序列化的实体类
Namespace-XML根元素的命名空间[XmlRoot(NamespaceXmlSerializerObject
ElementName-生成的XML元素的名称
Order-序列化和反序列化的顺序[XmlElement(ElementNameGuidID

[XmlElement(Order
NameAgeDateTimeTimeSoapFormatterObject.cs
WCF.ServiceLib.Serialization
{
用于演示Soap序列化的实体类
[Serializable]
SoapFormatterOjbect
GuidIDNameAgeDateTimeTimeBinaryFormatterObject.cs
WCF.ServiceLib.Serialization
{
用于演示Binary序列化的实体类
BinaryFormatterObject
GuidIDNameAgeDateTimeTimeDataContractJsonSerializerObject.cs
WCF.ServiceLib.Serialization
{
用于演示JSON序列化的实体类
DataContractJsonSerializerObject
GuidIDNameAgeDateTimeTime@H_17_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();
}


演示DataContractSerializer的序列化和反序列化
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;
}


演示XmlSerializer的序列化和反序列化
ShowXmlSerializer()
{
varxmlSerializerObject
API.XmlSerializerObjectXmlSerializerSystem.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;
}


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

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


演示BinaryFormatter的序列化和反序列化
ShowBinaryFormatter()
{
varbinaryFormatterObject
API.BinaryFormatterObjectBinaryFormatterSystem.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;
}


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

sr.ReadToEnd();
txtDataContractJsonSerializer.Text
MemoryStream(buffer);
vardataContractJsonSerializerObject2
API.DataContractJsonSerializerObject;
lblDataContractJsonSerializer.Text
dataContractJsonSerializerObject2.Name;
}
}
原文链接:https://www.f2er.com/json/290642.html

猜你在找的Json相关文章