化零为整WCF(9) - 序列化(DataContractSerializer, XmlSerializer, DataContractJsonSerializer-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

化零为整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
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifusing System.Runtime.Serialization; 
InBlock.gif 
InBlock.gifnamespace WCF.ServiceLib.Serialization 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 用于演示DataContract序列化的实体类 
InBlock.gif        /// </summary> 
InBlock.gif        // Namespace - 数据契约的命名空间 
InBlock.gif        [DataContract(Namespace = "http://webabcd.cnblogs.com/")] 
InBlock.gif        public class DataContractSerializerObject 
InBlock.gif        { 
InBlock.gif                // Name - 数据成员的名称 
InBlock.gif                // Order - 数据成员的序列化和反序列化的顺序 
InBlock.gif                [DataMember(Name = "UniqueID", Order = 0)] 
InBlock.gif                public Guid ID { get; set; } 
InBlock.gif 
InBlock.gif                [DataMember(Order = 1)] 
InBlock.gif                public string Name { get; set; } 
InBlock.gif 
InBlock.gif                [DataMember(Order = 2)] 
InBlock.gif                public int Age { get; set; } 
InBlock.gif 
InBlock.gif                [DataMember(Order = 3)] 
InBlock.gif                public DateTime Time { get; set; } 
InBlock.gif        } 
InBlock.gif}
 
XmlSerializerObject.cs
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifusing System.Runtime.Serialization; 
InBlock.gifusing System.Xml.Serialization; 
InBlock.gif 
InBlock.gifnamespace WCF.ServiceLib.Serialization 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 用于演示XML序列化的实体类 
InBlock.gif        /// </summary> 
InBlock.gif        // Namespace - XML 根元素的命名空间 
InBlock.gif        [XmlRoot(Namespace = "http://webabcd.cnblogs.com/")] 
InBlock.gif        public class XmlSerializerObject 
InBlock.gif        { 
InBlock.gif                // ElementName - 生成的 XML 元素的名称 
InBlock.gif                // Order - 序列化和反序列化的顺序 
InBlock.gif                [XmlElement(ElementName = "UniqueID", Order = 0)] 
InBlock.gif                public Guid ID { get; set; } 
InBlock.gif 
InBlock.gif                [XmlElement(Order = 1)] 
InBlock.gif                public string Name { get; set; } 
InBlock.gif 
InBlock.gif                [XmlElement(Order = 2)] 
InBlock.gif                public int Age { get; set; } 
InBlock.gif 
InBlock.gif                [XmlElement(Order = 3)] 
InBlock.gif                public DateTime Time { get; set; } 
InBlock.gif        } 
InBlock.gif}
 
SoapFormatterObject.cs
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifusing System.Runtime.Serialization; 
InBlock.gifusing System.Xml.Serialization; 
InBlock.gif 
InBlock.gifnamespace WCF.ServiceLib.Serialization 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 用于演示Soap序列化的实体类 
InBlock.gif        /// </summary> 
InBlock.gif        [Serializable] 
InBlock.gif        public class SoapFormatterOjbect 
InBlock.gif        { 
InBlock.gif                public Guid ID { get; set; } 
InBlock.gif                public string Name { get; set; } 
InBlock.gif                public int Age { get; set; } 
InBlock.gif                public DateTime Time { get; set; } 
InBlock.gif        } 
InBlock.gif}
 
BinaryFormatterObject.cs
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifusing System.Runtime.Serialization; 
InBlock.gifusing System.Xml.Serialization; 
InBlock.gif 
InBlock.gifnamespace WCF.ServiceLib.Serialization 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 用于演示Binary序列化的实体类 
InBlock.gif        /// </summary> 
InBlock.gif        [Serializable] 
InBlock.gif        public class BinaryFormatterObject 
InBlock.gif        { 
InBlock.gif                public Guid ID { get; set; } 
InBlock.gif                public string Name { get; set; } 
InBlock.gif                public int Age { get; set; } 
InBlock.gif                public DateTime Time { get; set; } 
InBlock.gif        } 
InBlock.gif}
 
DataContractJsonSerializerObject.cs
InBlock.gifusing System; 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Text; 
InBlock.gif 
InBlock.gifusing System.Runtime.Serialization; 
InBlock.gif 
InBlock.gifnamespace WCF.ServiceLib.Serialization 
InBlock.gif
InBlock.gif        /// <summary> 
InBlock.gif        /// 用于演示JSON序列化的实体类 
InBlock.gif        /// </summary> 
InBlock.gif        // Namespace - 数据契约的命名空间 
InBlock.gif        [DataContract(Namespace = "http://webabcd.cnblogs.com/")] 
InBlock.gif        public class DataContractJsonSerializerObject 
InBlock.gif        { 
InBlock.gif                // Name - 数据成员的名称 
InBlock.gif                // Order - 数据成员的序列化和反序列化的顺序 
InBlock.gif                [DataMember(Name = "UniqueID", Order = 0)] 
InBlock.gif                public Guid ID { get; set; } 
InBlock.gif 
InBlock.gif                [DataMember(Order = 1)] 
InBlock.gif                public string Name { get; set; } 
InBlock.gif 
InBlock.gif                [DataMember(Order = 2)] 
InBlock.gif                public int Age { get; set; } 
InBlock.gif 
InBlock.gif                [DataMember(Order = 3)] 
InBlock.gif                public DateTime Time { get; set; } 
InBlock.gif        } 
InBlock.gif}
 
 
2、演示
Sample.aspx.cs
InBlock.gifusing System; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Xml.Linq; 
InBlock.gif 
InBlock.gifusing System.IO; 
InBlock.gif 
InBlock.gifusing API = WCF.ServiceLib.Serialization; 
InBlock.gif 
InBlock.gifpublic partial class Serialization_Sample : System.Web.UI.Page 
InBlock.gif
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                // 演示DataContractSerializer的序列化和反序列化 
InBlock.gif                ShowDataContractSerializer(); 
InBlock.gif 
InBlock.gif                // 演示XmlSerializer的序列化和反序列化 
InBlock.gif                ShowXmlSerializer(); 
InBlock.gif 
InBlock.gif                // 演示SoapFormatter的序列化和反序列化 
InBlock.gif                ShowSoapFormatter(); 
InBlock.gif 
InBlock.gif                // 演示BinaryFormatter的序列化和反序列化 
InBlock.gif                ShowBinaryFormatter(); 
InBlock.gif 
InBlock.gif                // 演示DataContractJsonSerializer的序列化和反序列化 
InBlock.gif                ShowDataContractJsonSerializer(); 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        /// <summary> 
InBlock.gif        /// 演示DataContractSerializer的序列化和反序列化 
InBlock.gif        /// </summary> 
InBlock.gif        void ShowDataContractSerializer() 
InBlock.gif        { 
InBlock.gif                var dataContractSerializerObject = new API.DataContractSerializerObject { ID = Guid.NewGuid(), Name = "DataContractSerializer", Age = 28, Time = DateTime.Now }; 
InBlock.gif 
InBlock.gif                var serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(API.DataContractSerializerObject)); 
InBlock.gif 
InBlock.gif                // 序列化 
InBlock.gif                var ms = new MemoryStream(); 
InBlock.gif 
InBlock.gif                serializer.WriteObject(ms, dataContractSerializerObject); 
InBlock.gif 
InBlock.gif                ms.Position = 0; 
InBlock.gif                var sr = new StreamReader(ms); 
InBlock.gif                var str = sr.ReadToEnd(); 
InBlock.gif                txtDataContractSerializer.Text = str; 
InBlock.gif 
InBlock.gif 
InBlock.gif                // 反序列化 
InBlock.gif                var buffer = System.Text.Encoding.UTF8.GetBytes(str); 
InBlock.gif                var ms2 = new MemoryStream(buffer); 
InBlock.gif                var dataContractSerializerObject2 = serializer.ReadObject(ms2) as API.DataContractSerializerObject; 
InBlock.gif                lblDataContractSerializer.Text = dataContractSerializerObject2.Name; 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        /// <summary> 
InBlock.gif        /// 演示XmlSerializer的序列化和反序列化 
InBlock.gif        /// </summary> 
InBlock.gif        void ShowXmlSerializer() 
InBlock.gif        { 
InBlock.gif                var xmlSerializerObject = new API.XmlSerializerObject { ID = Guid.NewGuid(), Name = "XmlSerializer", Age = 28, Time = DateTime.Now }; 
InBlock.gif 
InBlock.gif                var serializer = new System.Xml.Serialization.XmlSerializer(typeof(API.XmlSerializerObject)); 
InBlock.gif 
InBlock.gif                // 序列化 
InBlock.gif                var ms = new MemoryStream(); 
InBlock.gif 
InBlock.gif                serializer.Serialize(ms, xmlSerializerObject); 
InBlock.gif 
InBlock.gif                ms.Position = 0; 
InBlock.gif                var sr = new StreamReader(ms); 
InBlock.gif                var str = sr.ReadToEnd(); 
InBlock.gif                txtXmlSerializer.Text = str; 
InBlock.gif 
InBlock.gif 
InBlock.gif                // 反序列化 
InBlock.gif                var buffer = System.Text.Encoding.UTF8.GetBytes(str); 
InBlock.gif                var ms2 = new MemoryStream(buffer); 
InBlock.gif                var xmlSerializerObject2 = serializer.Deserialize(ms2) as API.XmlSerializerObject; 
InBlock.gif                lblXmlSerializer.Text = xmlSerializerObject2.Name; 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        /// <summary> 
InBlock.gif        /// 演示SoapFormatter的序列化和反序列化 
InBlock.gif        /// </summary> 
InBlock.gif        void ShowSoapFormatter() 
InBlock.gif        { 
InBlock.gif                var soapFormatterOjbect = new API.SoapFormatterOjbect { ID = Guid.NewGuid(), Name = "ShowSoapFormatter", Age = 28, Time = DateTime.Now }; 
InBlock.gif 
InBlock.gif                var formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); 
InBlock.gif 
InBlock.gif                // 序列化 
InBlock.gif                var ms = new MemoryStream(); 
InBlock.gif 
InBlock.gif                formatter.Serialize(ms, soapFormatterOjbect); 
InBlock.gif 
InBlock.gif                ms.Position = 0; 
InBlock.gif                var str = System.Text.Encoding.UTF8.GetString(ms.GetBuffer()); 
InBlock.gif                txtSoapFormatter.Text = str; 
InBlock.gif 
InBlock.gif 
InBlock.gif                // 反序列化 
InBlock.gif                var buffer = System.Text.Encoding.UTF8.GetBytes(str); 
InBlock.gif                var ms2 = new MemoryStream(buffer); 
InBlock.gif                var soapFormatterOjbect2 = formatter.Deserialize(ms2) as API.SoapFormatterOjbect; 
InBlock.gif                lblSoapFormatter.Text = soapFormatterOjbect2.Name; 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        /// <summary> 
InBlock.gif        /// 演示BinaryFormatter的序列化和反序列化 
InBlock.gif        /// </summary> 
InBlock.gif        void ShowBinaryFormatter() 
InBlock.gif        { 
InBlock.gif                var binaryFormatterObject = new API.BinaryFormatterObject { ID = Guid.NewGuid(), Name = "BinaryFormatter", Age = 28, Time = DateTime.Now }; 
InBlock.gif 
InBlock.gif                var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); 
InBlock.gif 
InBlock.gif                // 序列化 
InBlock.gif                var ms = new MemoryStream(); 
InBlock.gif 
InBlock.gif                formatter.Serialize(ms, binaryFormatterObject); 
InBlock.gif 
InBlock.gif                ms.Position = 0; 
InBlock.gif                var buffer = ms.GetBuffer(); 
InBlock.gif                var str = System.Text.Encoding.UTF8.GetString(buffer); 
InBlock.gif                txtBinaryFormatter.Text = str; 
InBlock.gif 
InBlock.gif 
InBlock.gif                // 反序列化 
InBlock.gif                var ms2 = new MemoryStream(buffer); 
InBlock.gif                var binaryFormatterObject2 = formatter.Deserialize(ms2) as API.BinaryFormatterObject; 
InBlock.gif                lblBinaryFormatter.Text = binaryFormatterObject2.Name; 
InBlock.gif        } 
InBlock.gif 
InBlock.gif        /// <summary> 
InBlock.gif        /// 演示DataContractJsonSerializer的序列化和反序列化 
InBlock.gif        /// </summary> 
InBlock.gif        void ShowDataContractJsonSerializer() 
InBlock.gif        { 
InBlock.gif                var dataContractJsonSerializerObject = new API.DataContractJsonSerializerObject { ID = Guid.NewGuid(), Name = "DataContractJsonSerializer", Age = 28, Time = DateTime.Now }; 
InBlock.gif 
InBlock.gif                var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(API.DataContractJsonSerializerObject)); 
InBlock.gif 
InBlock.gif                // 序列化 
InBlock.gif                var ms = new MemoryStream(); 
InBlock.gif 
InBlock.gif                serializer.WriteObject(ms, dataContractJsonSerializerObject); 
InBlock.gif 
InBlock.gif                ms.Position = 0; 
InBlock.gif                var sr = new StreamReader(ms); 
InBlock.gif                var str = sr.ReadToEnd(); 
InBlock.gif                txtDataContractJsonSerializer.Text = str; 
InBlock.gif 
InBlock.gif 
InBlock.gif                // 反序列化 
InBlock.gif                var buffer = System.Text.Encoding.UTF8.GetBytes(str); 
InBlock.gif                var ms2 = new MemoryStream(buffer); 
InBlock.gif                var dataContractJsonSerializerObject2 = serializer.ReadObject(ms2) as API.DataContractJsonSerializerObject; 
InBlock.gif                lblDataContractJsonSerializer.Text = dataContractJsonSerializerObject2.Name; 
InBlock.gif        } 
InBlock.gif}
 

运行结果:
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,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享: