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 = )]
public Guid ID
{
set;
get;
} [DataMember(Order=)]
public string Name
{
set;
get;
} [DataMember(Order=)]
public int Age
{
set;
get;
} [DataMember(Order=)]
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 = )]
public Guid ID { set; get; } [DataMember(Order = )]
public string Name { set; get; } [DataMember(Order = )]
public int Age { set; get; } [DataMember(Order = )]
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=)]
public Guid ID { set; get; } [XmlElement(Order=)]
public string Name { set; get; } [XmlElement(Order=)]
public int Age { set; get; } [XmlElement(Order=)]
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 = ,
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 = ;
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 = ,
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 = ;
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 = ,
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 = ;
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 = ,
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 = ,
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 = ;
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);
}
}
}

wcf中序列化BinaryFormatter,DataContractJsonSerializer,DataContractSerializer,SoapFormatter,XmlSerializer的更多相关文章

  1. Entity Framework在WCF中序列化的问题

    问题描述 如果你在WCF中用Entity Framework来获取数据并返回实体对象,那么对下面的错误一定不陌生. 接收对 http://localhost:5115/ReService.svc 的 ...

  2. WCF中序列化(XML\JSON\Dt)

    序列化 是将对象转换为容易传输的格式的过程.例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间传输该对象.反之,反序列化根据流重新构造对象. 序列化描述了持久化 ...

  3. Entity Framework在WCF中序列化的问题(转)

    问题描述 如果你在WCF中用Entity Framework来获取数据并返回实体对象,那么对下面的错误一定不陌生. 接收对 http://localhost:5115/ReService.svc 的 ...

  4. WCF初探-25:WCF中使用XmlSerializer类

    前言 在上一篇WCF序列化和反序列化中,文章介绍了WCF序列化和反序列化的机制,虽然WCF针对序列化提供了默认的DataContractSerializer序列化引擎,但是WCF还支持其他的序列化引擎 ...

  5. 在 WCF 中使用高效的 BinaryFormatter 序列化

    本文将定义一个 WCF 终结点行为扩展,以在 WCF 中使用更高效的 BinaryFormatter 进行二进制序列化,并实现对是否使用传统二进制序列化功能的可配置. 介绍 实现步骤 使用方法 效果 ...

  6. 十五天精通WCF——第十二天 说说wcf中的那几种序列化

    我们都知道wcf是由信道栈组成的,在我们传输的参数走到传输信道层之前,先需要经过序列化的过程,也就是将参数序列化为message,这篇 我们就来说说这里的序列化,蛮有意思的,可能初学者也明白,在wcf ...

  7. .net的XML对象序列化VS WCF中xml序列化问题

    整理一下 .net 对象序列化注意事项: 1. 字段:必须是 public类型 2.属性:只读或者只写的属性不被序列化,只有 可读可写并且赋值的才可以 序列化: Someclass obj = new ...

  8. [转]十五天精通WCF——第十二天 说说wcf中的那几种序列化

    我们都知道wcf是由信道栈组成的,在我们传输的参数走到传输信道层之前,先需要经过序列化的过程,也就是将参数序列化为message,这篇 我们就来说说这里的序列化,蛮有意思的,可能初学者也明白,在wcf ...

  9. WCF技术剖析之十二:数据契约(Data Contract)和数据契约序列化器(DataContractSerializer)

    原文:WCF技术剖析之十二:数据契约(Data Contract)和数据契约序列化器(DataContractSerializer) [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济 ...

随机推荐

  1. 转:HtmlCxx用户手册

    1.1 简介 使用由KasperPeeters编写的强大的tree.h库文件,可以实现类似STL的DOM树遍历和导航. 打包好的Css解析器. 看似很像C++代码的C++代码(其实已不再是C++了) ...

  2. System.IO.Ports.SerialPort串口通信接收完整数据

    C#中使用System.IO.Ports.SerialPort进行串口通信网上资料也很多,但都没有提及一些细节: 比如 串口有时候并不会一次性把你想要的数据全部传输给你,可能会分为1次,2次,3次分别 ...

  3. Go -- 实现二叉搜索树

    树: https://suanfa.herokuapp.com/3%E6%A0%91/binarytree/ 数据结构 首先我们定义需要的数据结构.注意,TreeNode的左右节点都是*TreeNod ...

  4. Javascript:如何调用全局变量?

    怎样使用全局变量呢? window.globalVariableName 参考: https://blog.csdn.net/zyz511919766/article/details/7276089

  5. 关于IP地址与MAC地址(网卡硬件地址)的区别小谈

    IP地址是指Internet协议使用的地址,而MAC地址是Ethernet协议使用的地址. IP地址与MAC地址之间并没有什么必然的联系,MAC地址是Ethernet NIC(网卡)上带的地址,为48 ...

  6. C#总结复习5(需要进一步复习)

    第十五章 接口 1.接口: C++中允许多继承没有接口的概念.而java与C#中有,因为C#中 是单继承多接口. 所谓的接口,其实和抽象类.方法相似.都只有一个空方法.其本身不可以为基类,但是允许被其 ...

  7. MySql视频教程——百度云下载路径

    百度云分享MySql视频教程给大家.祝大家事业进步! MySql视频教程:http://pan.baidu.com/s/1gdCHX79 password:n46i

  8. 走入asp.net mvc不归路:[2]控制器概览

    asp.net mvc中最灵活的地方就是控制器,这里可以验证数据,可以跳转视图,还可以访问数据库等等.所以,我们要先从这里说起. 1 控制器就是继承了Controller的类,一般来说,类名后面都会增 ...

  9. 阿里云serverMySQL无法连接问题解决纪实

    作者:fbysss QQ:溜酒酒吧酒吧吾散 blog:blog.csdn.net/fbysss 声明:本文由fbysss原创,转载请注明出处 背景: 在调试程序的时候,发现数据库訪问相关的环节出现错误 ...

  10. MaterialImageLoading

    https://github.com/eltld/MaterialImageLoading