wcf中序列化BinaryFormatter,DataContractJsonSerializer,DataContractSerializer,SoapFormatter,XmlSerializer
using System; using System.Runtime.Serialization; using System.Xml.Serialization; namespace Larryle.Wcf.ServiceContectData.Serialization { [Serializable] public class BinaryFormatterObject { public Guid ID { get; set; } public string Name { set; get; } public int Age { set; get; } public DateTime Time { set; get; } } }
using System; using System.Runtime.Serialization; namespace Larryle.Wcf.ServiceContectData.Serialization { [DataContract(Namespace = "http://www.larryle.com/")] public class DataContractJsonSerializerObject { [DataMember(Name = "UniqueID", Order = 0)] public Guid ID { set; get; } [DataMember(Order=1)] public string Name { set; get; } [DataMember(Order=2)] public int Age { set; get; } [DataMember(Order=3)] public DateTime Time { set; get; } } }
using System; using System.Runtime.Serialization; namespace Larryle.Wcf.ServiceContectData.Serialization { [DataContract(Namespace = "http://larryle.com")] public class DataContractSerializerObject { [DataMember(Name = "UniqueID", Order = 0)] public Guid ID { set; get; } [DataMember(Order = 1)] public string Name { set; get; } [DataMember(Order = 2)] public int Age { set; get; } [DataMember(Order = 3)] public DateTime Time { set; get; } } }
using System; using System.Xml.Serialization; namespace Larryle.Wcf.ServiceContectData.Serialization { [Serializable] public class SoapFormatterObject { public Guid ID { set; get; } public string Name { set; get; } public int Age { set; get; } public DateTime Time { set; get; } } }
using System; using System.Xml.Serialization; namespace Larryle.Wcf.ServiceContectData.Serialization { [XmlRoot(Namespace = "http://larryle.com")] public class XmlSerializerObject { [XmlElement(ElementName="UniqueID",Order=0)] public Guid ID { set; get; } [XmlElement(Order=1)] public string Name { set; get; } [XmlElement(Order=2)] public int Age { set; get; } [XmlElement(Order=3)] public DateTime Time { set; get; } } }
using System; using System.IO; using System.ServiceModel; using Larryle.Wcf.ServiceContectData.Serialization; namespace Larryle.WcfConsoleHost.Serialization { public class Sample { public void Launch() { ShowBinaryFormatter(); ShowDataContractJsonSerializer(); ShowDataContractSerializer(); ShowSoapFormatterObject(); ShowXmlSerializerObject(); ShowXmlSerializerObject(); } public void ShowBinaryFormatter() { BinaryFormatterObject binaryobject = new BinaryFormatterObject { ID = Guid.NewGuid(), Name = "BinaryFormatter", Age = 28, Time = DateTime.Now }; System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); MemoryStream ms = new MemoryStream(); binaryformatter.Serialize(ms, binaryobject); ms.Position = 0; Byte[] by = ms.GetBuffer(); string str = System.Text.Encoding.UTF8.GetString(by); Console.WriteLine("序列化BinaryFormatter"); Console.WriteLine(str); MemoryStream ms2 = new MemoryStream(by); BinaryFormatterObject binaryboject2 = binaryformatter.Deserialize(ms2) as BinaryFormatterObject; Console.WriteLine("反序列化BinaryFormatter"); Console.WriteLine(binaryboject2.Name); } public void ShowDataContractJsonSerializer() { DataContractJsonSerializerObject jsonobjec = new DataContractJsonSerializerObject { ID = Guid.NewGuid(), Name = "DataContractJsonSerializer", Age = 28, Time = DateTime.Now }; System.Runtime.Serialization.Json.DataContractJsonSerializer jsonserializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(DataContractJsonSerializerObject)); MemoryStream ms = new MemoryStream(); jsonserializer.WriteObject(ms, jsonobjec); ms.Position = 0; StreamReader re = new StreamReader(ms); string str = re.ReadToEnd(); Console.WriteLine("序列化DataContractJsonSerializer"); Console.WriteLine(str); byte[] by = System.Text.Encoding.UTF8.GetBytes(str); MemoryStream ms2 = new MemoryStream(by); DataContractJsonSerializerObject obj2 = jsonserializer.ReadObject(ms2) as DataContractJsonSerializerObject; Console.WriteLine(obj2.Name); } public void ShowDataContractSerializer() { DataContractSerializerObject datacontraceobj = new DataContractSerializerObject { ID = Guid.NewGuid(), Name = "DataContractSerializer", Age = 28, Time = DateTime.Now }; System.Runtime.Serialization.DataContractSerializer datacontrace = new System.Runtime.Serialization.DataContractSerializer(typeof(DataContractSerializerObject)); MemoryStream ms = new MemoryStream(); datacontrace.WriteObject(ms, datacontraceobj); ms.Position = 0; StreamReader sr = new StreamReader(ms); string str = sr.ReadToEnd(); Console.WriteLine("序列化DataContractSerializer"); Console.WriteLine(str); Byte[] by = System.Text.Encoding.UTF8.GetBytes(str); MemoryStream ms2 = new MemoryStream(by); DataContractSerializerObject dataconatraceobj2 = datacontrace.ReadObject(ms2) as DataContractSerializerObject; Console.WriteLine(dataconatraceobj2.Name); } public void ShowSoapFormatterObject() { SoapFormatterObject soapobj = new SoapFormatterObject { ID = Guid.NewGuid(), Name = "DataContractJsonSerializer", Age = 28, Time = DateTime.Now }; System.Runtime.Serialization.Formatters.Soap.SoapFormatter soapserializer = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter(); MemoryStream sm = new MemoryStream(); soapserializer.Serialize(sm, soapobj); string str = System.Text.Encoding.UTF8.GetString(sm.GetBuffer()); Console.WriteLine("序列化SoapFormatterObject"); Console.WriteLine(str); byte[] by = System.Text.Encoding.UTF8.GetBytes(str); MemoryStream ms = new MemoryStream(by); SoapFormatterObject soapobj2 = soapserializer.Deserialize(ms) as SoapFormatterObject; Console.WriteLine(soapobj2.Name); } public void ShowXmlSerializerObject() { XmlSerializerObject xmlobj = new XmlSerializerObject { ID = Guid.NewGuid(), Name = "DataContractJsonSerializer", Age = 28, Time = DateTime.Now }; System.Xml.Serialization.XmlSerializer xmlerialzer = new System.Xml.Serialization.XmlSerializer(typeof(XmlSerializerObject)); MemoryStream ms = new MemoryStream(); xmlerialzer.Serialize(ms, xmlobj); ms.Position = 0; StreamReader sr = new StreamReader(ms); string str = sr.ReadToEnd(); Console.WriteLine("序列化XmlSerializerObject"); Console.WriteLine(str); byte[] by = System.Text.Encoding.UTF8.GetBytes(str); MemoryStream ms2 = new MemoryStream(by); XmlSerializerObject xmlobj2 = xmlerialzer.Deserialize(ms2) as XmlSerializerObject; Console.WriteLine(xmlobj2.Name); } } }