[索引页]
[源码下载]
化零为整WCF(9) - 序列化(DataContractSerializer, XmlSerializer, DataContractJsonSerializer, SoapFormatter, BinaryFormatter)
作者:
webabcd
介绍
WCF(Windows Communication Foundation) - 序列化:本文分别以DataContractSerializer, XmlSerializer, DataContractJsonSerializer, SoapFormatter, BinaryFormatter为例。
示例
1、服务
DataContractSerializerObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Runtime.Serialization;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace WCF.ServiceLib.Serialization
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 用于演示DataContract序列化的实体类
/// </summary>
// Namespace - 数据契约的命名空间
[DataContract(Namespace = "http://webabcd.cnblogs.com/")]
public class DataContractSerializerObject
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Name - 数据成员的名称
// Order - 数据成员的序列化和反序列化的顺序
[DataMember(Name = "UniqueID", Order = 0)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Guid ID
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember(Order = 1)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Name
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember(Order = 2)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Age
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember(Order = 3)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public DateTime Time
{ get; set; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
XmlSerializerObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Runtime.Serialization;
using System.Xml.Serialization;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace WCF.ServiceLib.Serialization
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 用于演示XML序列化的实体类
/// </summary>
// Namespace - XML 根元素的命名空间
[XmlRoot(Namespace = "http://webabcd.cnblogs.com/")]
public class XmlSerializerObject
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// ElementName - 生成的 XML 元素的名称
// Order - 序列化和反序列化的顺序
[XmlElement(ElementName = "UniqueID", Order = 0)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Guid ID
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[XmlElement(Order = 1)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Name
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[XmlElement(Order = 2)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Age
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[XmlElement(Order = 3)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public DateTime Time
{ get; set; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SoapFormatterObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Runtime.Serialization;
using System.Xml.Serialization;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace WCF.ServiceLib.Serialization
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 用于演示Soap序列化的实体类
/// </summary>
[Serializable]
public class SoapFormatterOjbect
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Guid ID
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Name
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Age
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public DateTime Time
{ get; set; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BinaryFormatterObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Runtime.Serialization;
using System.Xml.Serialization;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace WCF.ServiceLib.Serialization
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 用于演示Binary序列化的实体类
/// </summary>
[Serializable]
public class BinaryFormatterObject
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Guid ID
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Name
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Age
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public DateTime Time
{ get; set; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DataContractJsonSerializerObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.Runtime.Serialization;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace WCF.ServiceLib.Serialization
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 用于演示JSON序列化的实体类
/// </summary>
// Namespace - 数据契约的命名空间
[DataContract(Namespace = "http://webabcd.cnblogs.com/")]
public class DataContractJsonSerializerObject
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Name - 数据成员的名称
// Order - 数据成员的序列化和反序列化的顺序
[DataMember(Name = "UniqueID", Order = 0)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Guid ID
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember(Order = 1)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public string Name
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember(Order = 2)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public int Age
{ get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DataMember(Order = 3)]
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public DateTime Time
{ get; set; }
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using System.IO;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using API = WCF.ServiceLib.Serialization;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public partial class Serialization_Sample : System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// 演示DataContractSerializer的序列化和反序列化
ShowDataContractSerializer();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 演示XmlSerializer的序列化和反序列化
ShowXmlSerializer();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 演示SoapFormatter的序列化和反序列化
ShowSoapFormatter();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 演示BinaryFormatter的序列化和反序列化
ShowBinaryFormatter();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 演示DataContractJsonSerializer的序列化和反序列化
ShowDataContractJsonSerializer();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 演示DataContractSerializer的序列化和反序列化
/// </summary>
void ShowDataContractSerializer()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var dataContractSerializerObject = new API.DataContractSerializerObject
{ ID = Guid.NewGuid(), Name = "DataContractSerializer", Age = 28, Time = DateTime.Now };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(API.DataContractSerializerObject));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 序列化
var ms = new MemoryStream();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
serializer.WriteObject(ms, dataContractSerializerObject);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ms.Position = 0;
var sr = new StreamReader(ms);
var str = sr.ReadToEnd();
txtDataContractSerializer.Text = str;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 反序列化
var buffer = System.Text.Encoding.UTF8.GetBytes(str);
var ms2 = new MemoryStream(buffer);
var dataContractSerializerObject2 = serializer.ReadObject(ms2) as API.DataContractSerializerObject;
lblDataContractSerializer.Text = dataContractSerializerObject2.Name;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 演示XmlSerializer的序列化和反序列化
/// </summary>
void ShowXmlSerializer()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var xmlSerializerObject = new API.XmlSerializerObject
{ ID = Guid.NewGuid(), Name = "XmlSerializer", Age = 28, Time = DateTime.Now };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(API.XmlSerializerObject));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 序列化
var ms = new MemoryStream();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
serializer.Serialize(ms, xmlSerializerObject);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ms.Position = 0;
var sr = new StreamReader(ms);
var str = sr.ReadToEnd();
txtXmlSerializer.Text = str;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 反序列化
var buffer = System.Text.Encoding.UTF8.GetBytes(str);
var ms2 = new MemoryStream(buffer);
var xmlSerializerObject2 = serializer.Deserialize(ms2) as API.XmlSerializerObject;
lblXmlSerializer.Text = xmlSerializerObject2.Name;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 演示SoapFormatter的序列化和反序列化
/// </summary>
void ShowSoapFormatter()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var soapFormatterOjbect = new API.SoapFormatterOjbect
{ ID = Guid.NewGuid(), Name = "ShowSoapFormatter", Age = 28, Time = DateTime.Now };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 序列化
var ms = new MemoryStream();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
formatter.Serialize(ms, soapFormatterOjbect);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ms.Position = 0;
var str = System.Text.Encoding.UTF8.GetString(ms.GetBuffer());
txtSoapFormatter.Text = str;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 反序列化
var buffer = System.Text.Encoding.UTF8.GetBytes(str);
var ms2 = new MemoryStream(buffer);
var soapFormatterOjbect2 = formatter.Deserialize(ms2) as API.SoapFormatterOjbect;
lblSoapFormatter.Text = soapFormatterOjbect2.Name;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 演示BinaryFormatter的序列化和反序列化
/// </summary>
void ShowBinaryFormatter()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var binaryFormatterObject = new API.BinaryFormatterObject
{ ID = Guid.NewGuid(), Name = "BinaryFormatter", Age = 28, Time = DateTime.Now };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 序列化
var ms = new MemoryStream();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
formatter.Serialize(ms, binaryFormatterObject);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ms.Position = 0;
var buffer = ms.GetBuffer();
var str = System.Text.Encoding.UTF8.GetString(buffer);
txtBinaryFormatter.Text = str;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 反序列化
var ms2 = new MemoryStream(buffer);
var binaryFormatterObject2 = formatter.Deserialize(ms2) as API.BinaryFormatterObject;
lblBinaryFormatter.Text = binaryFormatterObject2.Name;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 演示DataContractJsonSerializer的序列化和反序列化
/// </summary>
void ShowDataContractJsonSerializer()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
var dataContractJsonSerializerObject = new API.DataContractJsonSerializerObject
{ ID = Guid.NewGuid(), Name = "DataContractJsonSerializer", Age = 28, Time = DateTime.Now };
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(API.DataContractJsonSerializerObject));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 序列化
var ms = new MemoryStream();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
serializer.WriteObject(ms, dataContractJsonSerializerObject);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ms.Position = 0;
var sr = new StreamReader(ms);
var str = sr.ReadToEnd();
txtDataContractJsonSerializer.Text = str;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
// 反序列化
var buffer = System.Text.Encoding.UTF8.GetBytes(str);
var ms2 = new MemoryStream(buffer);
var dataContractJsonSerializerObject2 = serializer.ReadObject(ms2) as API.DataContractJsonSerializerObject;
lblDataContractJsonSerializer.Text = dataContractJsonSerializerObject2.Name;
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行结果:
DataContractSerializer
XmlSerializer
ShowSoapFormatter
BinaryFormatter
DataContractJsonSerializer
OK
[源码下载]