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(Order1stringName2intAge3DateTimeTime
}
}
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(Order1stringName2intAge3DateTimeTime
}
}
XmlSerializerObject.cs
System.Runtime.Serialization;
System.Xml.Serialization;
WCF.ServiceLib.Serialization
{
用于演示XML序列化的实体类
Namespace-XML根元素的命名空间[XmlRoot(NamespaceXmlSerializerObject
ElementName-生成的XML元素的名称
Order-序列化和反序列化的顺序[XmlElement(ElementNameGuidID
[XmlElement(OrderNameAgeDateTimeTimeSoapFormatterObject.cs
WCF.ServiceLib.Serialization
{
用于演示Soap序列化的实体类
[Serializable]
SoapFormatterOjbect
GuidIDNameAgeDateTimeTimeBinaryFormatterObject.cs
WCF.ServiceLib.Serialization
{
用于演示Binary序列化的实体类
BinaryFormatterObject
GuidIDNameAgeDateTimeTimeDataContractJsonSerializerObject.cs
WCF.ServiceLib.Serialization
{
用于演示JSON序列化的实体类
DataContractJsonSerializerObject
GuidIDNameAgeDateTimeTime@H_147_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()
{
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;
}
演示XmlSerializer的序列化和反序列化
ShowXmlSerializer()
{
varxmlSerializerObjectAPI.XmlSerializerObject@H_404_510@XmlSerializerSystem.Xml.Serialization.XmlSerializer((API.XmlSerializerObject));
MemoryStream();
serializer.Serialize(ms,xmlSerializerObject);
ms.Positionsr.ReadToEnd();
txtXmlSerializer.TextMemoryStream(buffer);
varxmlSerializerObject2serializer.Deserialize(ms2)API.XmlSerializerObject;
lblXmlSerializer.TextxmlSerializerObject2.Name;
}
演示SoapFormatter的序列化和反序列化
ShowSoapFormatter()
{
varsoapFormatterOjbectAPI.SoapFormatterOjbectShowSoapFormatter;
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;
}
演示BinaryFormatter的序列化和反序列化
ShowBinaryFormatter()
{
varbinaryFormatterObjectAPI.BinaryFormatterObjectBinaryFormatterSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
;
varbufferms.GetBuffer();
varstrSystem.Text.Encoding.UTF8.GetString(buffer);
txtBinaryFormatter.Textvarms2MemoryStream(buffer);
varbinaryFormatterObject2API.BinaryFormatterObject;
lblBinaryFormatter.TextbinaryFormatterObject2.Name;
}
演示DataContractJsonSerializer的序列化和反序列化
ShowDataContractJsonSerializer()
{
vardataContractJsonSerializerObjectAPI.DataContractJsonSerializerObjectDataContractJsonSerializerSystem.Runtime.Serialization.Json.DataContractJsonSerializer((API.DataContractJsonSerializerObject));
sr.ReadToEnd();
txtDataContractJsonSerializer.TextMemoryStream(buffer);
vardataContractJsonSerializerObject2API.DataContractJsonSerializerObject;
lblDataContractJsonSerializer.TextdataContractJsonSerializerObject2.Name;
} }
原文链接:https://www.f2er.com/json/290642.htmlSystem.Xml.Serialization;
WCF.ServiceLib.Serialization
{
用于演示XML序列化的实体类
Namespace-XML根元素的命名空间[XmlRoot(NamespaceXmlSerializerObject
ElementName-生成的XML元素的名称
Order-序列化和反序列化的顺序[XmlElement(ElementNameGuidID
[XmlElement(OrderNameAgeDateTimeTimeSoapFormatterObject.cs
WCF.ServiceLib.Serialization
{
用于演示Soap序列化的实体类
[Serializable]
SoapFormatterOjbect
GuidIDNameAgeDateTimeTimeBinaryFormatterObject.cs
WCF.ServiceLib.Serialization
{
用于演示Binary序列化的实体类
BinaryFormatterObject
GuidIDNameAgeDateTimeTimeDataContractJsonSerializerObject.cs
WCF.ServiceLib.Serialization
{
用于演示JSON序列化的实体类
DataContractJsonSerializerObject
GuidIDNameAgeDateTimeTime@H_147_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()
{
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;
}
演示XmlSerializer的序列化和反序列化
ShowXmlSerializer()
{
varxmlSerializerObjectAPI.XmlSerializerObject@H_404_510@XmlSerializerSystem.Xml.Serialization.XmlSerializer((API.XmlSerializerObject));
MemoryStream();
serializer.Serialize(ms,xmlSerializerObject);
ms.Positionsr.ReadToEnd();
txtXmlSerializer.TextMemoryStream(buffer);
varxmlSerializerObject2serializer.Deserialize(ms2)API.XmlSerializerObject;
lblXmlSerializer.TextxmlSerializerObject2.Name;
}
演示SoapFormatter的序列化和反序列化
ShowSoapFormatter()
{
varsoapFormatterOjbectAPI.SoapFormatterOjbectShowSoapFormatter;
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;
}
演示BinaryFormatter的序列化和反序列化
ShowBinaryFormatter()
{
varbinaryFormatterObjectAPI.BinaryFormatterObjectBinaryFormatterSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
;
varbufferms.GetBuffer();
varstrSystem.Text.Encoding.UTF8.GetString(buffer);
txtBinaryFormatter.Textvarms2MemoryStream(buffer);
varbinaryFormatterObject2API.BinaryFormatterObject;
lblBinaryFormatter.TextbinaryFormatterObject2.Name;
}
演示DataContractJsonSerializer的序列化和反序列化
ShowDataContractJsonSerializer()
{
vardataContractJsonSerializerObjectAPI.DataContractJsonSerializerObjectDataContractJsonSerializerSystem.Runtime.Serialization.Json.DataContractJsonSerializer((API.DataContractJsonSerializerObject));
sr.ReadToEnd();
txtDataContractJsonSerializer.TextMemoryStream(buffer);
vardataContractJsonSerializerObject2API.DataContractJsonSerializerObject;
lblDataContractJsonSerializer.TextdataContractJsonSerializerObject2.Name;
} }