化零为整WCF(9) - 序列化
作者: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;using System.Runtime.Serialization;
namespace WCF.ServiceLib.Serialization
{
/**//// <summary>
/// 用于演示DataContract序列化的实体类
/// </summary>
// Namespace - 数据契约的命名空间
[DataContract(Namespace = "http://webabcd.cnblogs.com/")]
public class DataContractSerializerObject
{
// Name - 数据成员的名称
// Order - 数据成员的序列化和反序列化的顺序
[DataMember(Name = "UniqueID", Order = 0)]
public Guid ID { get; set; }[DataMember(Order = 1)]
public string Name { get; set; }[DataMember(Order = 2)]
public int Age { get; set; }[DataMember(Order = 3)]
public DateTime Time { get; set; }
}
}XmlSerializerObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using System.Runtime.Serialization;
using System.Xml.Serialization;namespace WCF.ServiceLib.Serialization
{
/**//// <summary>
/// 用于演示XML序列化的实体类
/// </summary>
// Namespace - XML 根元素的命名空间
[XmlRoot(Namespace = "http://webabcd.cnblogs.com/")]
public class XmlSerializerObject
{
// ElementName - 生成的 XML 元素的名称
// Order - 序列化和反序列化的顺序
[XmlElement(ElementName = "UniqueID", Order = 0)]
public Guid ID { get; set; }[XmlElement(Order = 1)]
public string Name { get; set; }[XmlElement(Order = 2)]
public int Age { get; set; }[XmlElement(Order = 3)]
public DateTime Time { get; set; }
}
}SoapFormatterObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using System.Runtime.Serialization;
using System.Xml.Serialization;namespace WCF.ServiceLib.Serialization
{
/**//// <summary>
/// 用于演示Soap序列化的实体类
/// </summary>
[Serializable]
public class SoapFormatterOjbect
{
public Guid ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public DateTime Time { get; set; }
}
}BinaryFormatterObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using System.Runtime.Serialization;
using System.Xml.Serialization;namespace WCF.ServiceLib.Serialization
{
/**//// <summary>
/// 用于演示Binary序列化的实体类
/// </summary>
[Serializable]
public class BinaryFormatterObject
{
public Guid ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public DateTime Time { get; set; }
}
}DataContractJsonSerializerObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;using System.Runtime.Serialization;
namespace WCF.ServiceLib.Serialization
{
/**//// <summary>
/// 用于演示JSON序列化的实体类
/// </summary>
// Namespace - 数据契约的命名空间
[DataContract(Namespace = "http://webabcd.cnblogs.com/")]
public class DataContractJsonSerializerObject
{
// Name - 数据成员的名称
// Order - 数据成员的序列化和反序列化的顺序
[DataMember(Name = "UniqueID", Order = 0)]
public Guid ID { get; set; }[DataMember(Order = 1)]
public string Name { get; set; }[DataMember(Order = 2)]
public int Age { get; set; }[DataMember(Order = 3)]
public DateTime Time { get; set; }
}
}
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;using System.IO;
using API = WCF.ServiceLib.Serialization;
public partial class Serialization_Sample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 演示DataContractSerializer的序列化和反序列化
ShowDataContractSerializer();// 演示XmlSerializer的序列化和反序列化
ShowXmlSerializer();// 演示SoapFormatter的序列化和反序列化
ShowSoapFormatter();// 演示BinaryFormatter的序列化和反序列化
ShowBinaryFormatter();// 演示DataContractJsonSerializer的序列化和反序列化
ShowDataContractJsonSerializer();
}/**//// <summary>
/// 演示DataContractSerializer的序列化和反序列化
/// </summary>
&n
补充:综合编程 , 其他综合 ,