化零为整WCF(9) - 序列化(DataContractSerializer, XmlSerializer, DataContractJsonSerializer

简介:
[索引页]
[源码下载] 


化零为整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; 
 
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> 
         void ShowDataContractSerializer() 
        { 
                var dataContractSerializerObject =  new API.DataContractSerializerObject { ID = Guid.NewGuid(), Name =  "DataContractSerializer", Age = 28, Time = DateTime.Now }; 
 
                var serializer =  new System.Runtime.Serialization.DataContractSerializer( typeof(API.DataContractSerializerObject)); 
 
                 // 序列化 
                var ms =  new MemoryStream(); 
 
                serializer.WriteObject(ms, dataContractSerializerObject); 
 
                ms.Position = 0; 
                var sr =  new StreamReader(ms); 
                var str = sr.ReadToEnd(); 
                txtDataContractSerializer.Text = str; 
 
 
                 // 反序列化 
                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; 
        } 
 
         /// <summary> 
         /// 演示XmlSerializer的序列化和反序列化 
         /// </summary> 
         void ShowXmlSerializer() 
        { 
                var xmlSerializerObject =  new API.XmlSerializerObject { ID = Guid.NewGuid(), Name =  "XmlSerializer", Age = 28, Time = DateTime.Now }; 
 
                var serializer =  new System.Xml.Serialization.XmlSerializer( typeof(API.XmlSerializerObject)); 
 
                 // 序列化 
                var ms =  new MemoryStream(); 
 
                serializer.Serialize(ms, xmlSerializerObject); 
 
                ms.Position = 0; 
                var sr =  new StreamReader(ms); 
                var str = sr.ReadToEnd(); 
                txtXmlSerializer.Text = str; 
 
 
                 // 反序列化 
                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; 
        } 
 
         /// <summary> 
         /// 演示SoapFormatter的序列化和反序列化 
         /// </summary> 
         void ShowSoapFormatter() 
        { 
                var soapFormatterOjbect =  new API.SoapFormatterOjbect { ID = Guid.NewGuid(), Name =  "ShowSoapFormatter", Age = 28, Time = DateTime.Now }; 
 
                var formatter =  new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); 
 
                 // 序列化 
                var ms =  new MemoryStream(); 
 
                formatter.Serialize(ms, soapFormatterOjbect); 
 
                ms.Position = 0; 
                var str = System.Text.Encoding.UTF8.GetString(ms.GetBuffer()); 
                txtSoapFormatter.Text = str; 
 
 
                 // 反序列化 
                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; 
        } 
 
         /// <summary> 
         /// 演示BinaryFormatter的序列化和反序列化 
         /// </summary> 
         void ShowBinaryFormatter() 
        { 
                var binaryFormatterObject =  new API.BinaryFormatterObject { ID = Guid.NewGuid(), Name =  "BinaryFormatter", Age = 28, Time = DateTime.Now }; 
 
                var formatter =  new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); 
 
                 // 序列化 
                var ms =  new MemoryStream(); 
 
                formatter.Serialize(ms, binaryFormatterObject); 
 
                ms.Position = 0; 
                var buffer = ms.GetBuffer(); 
                var str = System.Text.Encoding.UTF8.GetString(buffer); 
                txtBinaryFormatter.Text = str; 
 
 
                 // 反序列化 
                var ms2 =  new MemoryStream(buffer); 
                var binaryFormatterObject2 = formatter.Deserialize(ms2)  as API.BinaryFormatterObject; 
                lblBinaryFormatter.Text = binaryFormatterObject2.Name; 
        } 
 
         /// <summary> 
         /// 演示DataContractJsonSerializer的序列化和反序列化 
         /// </summary> 
         void ShowDataContractJsonSerializer() 
        { 
                var dataContractJsonSerializerObject =  new API.DataContractJsonSerializerObject { ID = Guid.NewGuid(), Name =  "DataContractJsonSerializer", Age = 28, Time = DateTime.Now }; 
 
                var serializer =  new System.Runtime.Serialization.Json.DataContractJsonSerializer( typeof(API.DataContractJsonSerializerObject)); 
 
                 // 序列化 
                var ms =  new MemoryStream(); 
 
                serializer.WriteObject(ms, dataContractJsonSerializerObject); 
 
                ms.Position = 0; 
                var sr =  new StreamReader(ms); 
                var str = sr.ReadToEnd(); 
                txtDataContractJsonSerializer.Text = str; 
 
 
                 // 反序列化 
                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; 
        } 
}
 

运行结果:
DataContractSerializer 
<DataContractSerializerObject xmlns="http://webabcd.cnblogs.com/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><UniqueID>86148742-ef08-4d78-acab-ce393ea8b885</UniqueID><Name>DataContractSerializer</Name><Age>28</Age><Time>2008-04-24T09:06:23.875+08:00</Time></DataContractSerializerObject>
 
XmlSerializer
<?xml version="1.0"?> <XmlSerializerObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://webabcd.cnblogs.com/"> <UniqueID>15efa47f-9532-4e91-8c00-3097ea976106</UniqueID> <Name>XmlSerializer</Name> <Age>28</Age> <Time>2008-04-24T09:06:24.109375+08:00</Time> </XmlSerializerObject>
 
ShowSoapFormatter 
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <a1:SoapFormatterOjbect id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/WCF.ServiceLib.Serialization/WCF.ServiceLib%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull"> <_x003C_ID_x003E_k__BackingField> <_a>1444890659</_a> <_b>-27168</_b> <_c>20156</_c> <_d>131</_d> <_e>247</_e> <_f>146</_f> <_g>29</_g> <_h>18</_h> <_i>59</_i> <_j>58</_j> <_k>189</_k> </_x003C_ID_x003E_k__BackingField> <_x003C_Name_x003E_k__BackingField id="ref-3">ShowSoapFormatter</_x003C_Name_x003E_k__BackingField> <_x003C_Age_x003E_k__BackingField>28</_x003C_Age_x003E_k__BackingField> <_x003C_Time_x003E_k__BackingField>2008-04-24T09:06:24.5000000+08:00</_x003C_Time_x003E_k__BackingField> </a1:SoapFormatterOjbect> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
 
BinaryFormatter
���� EWCF.ServiceLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null2WCF.ServiceLib.Serialization.BinaryFormatterObject<ID>k__BackingField<Name>k__BackingField<Age>k__BackingField<Time>k__BackingField System.Guid ���� System.Guid _a_b_c_d_e_f_g_h_i_j_k��y�u��I����]F�BinaryFormatter�U�tʈ
 
DataContractJsonSerializer 
{"UniqueID":"f4173d7d-9555-4dcb-8db7-62b85315d3f1","Name":"DataContractJsonSerializer","Age":28,"Time":"\/Date(1208999184531+0800)\/"}
 
 
 



     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/344147 ,如需转载请自行联系原作者

相关文章

热门文章

最新文章