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 = )]
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的更多相关文章
- Entity Framework在WCF中序列化的问题
问题描述 如果你在WCF中用Entity Framework来获取数据并返回实体对象,那么对下面的错误一定不陌生. 接收对 http://localhost:5115/ReService.svc 的 ...
- WCF中序列化(XML\JSON\Dt)
序列化 是将对象转换为容易传输的格式的过程.例如,可以序列化一个对象,然后使用 HTTP 通过 Internet 在客户端和服务器之间传输该对象.反之,反序列化根据流重新构造对象. 序列化描述了持久化 ...
- Entity Framework在WCF中序列化的问题(转)
问题描述 如果你在WCF中用Entity Framework来获取数据并返回实体对象,那么对下面的错误一定不陌生. 接收对 http://localhost:5115/ReService.svc 的 ...
- WCF初探-25:WCF中使用XmlSerializer类
前言 在上一篇WCF序列化和反序列化中,文章介绍了WCF序列化和反序列化的机制,虽然WCF针对序列化提供了默认的DataContractSerializer序列化引擎,但是WCF还支持其他的序列化引擎 ...
- 在 WCF 中使用高效的 BinaryFormatter 序列化
本文将定义一个 WCF 终结点行为扩展,以在 WCF 中使用更高效的 BinaryFormatter 进行二进制序列化,并实现对是否使用传统二进制序列化功能的可配置. 介绍 实现步骤 使用方法 效果 ...
- 十五天精通WCF——第十二天 说说wcf中的那几种序列化
我们都知道wcf是由信道栈组成的,在我们传输的参数走到传输信道层之前,先需要经过序列化的过程,也就是将参数序列化为message,这篇 我们就来说说这里的序列化,蛮有意思的,可能初学者也明白,在wcf ...
- .net的XML对象序列化VS WCF中xml序列化问题
整理一下 .net 对象序列化注意事项: 1. 字段:必须是 public类型 2.属性:只读或者只写的属性不被序列化,只有 可读可写并且赋值的才可以 序列化: Someclass obj = new ...
- [转]十五天精通WCF——第十二天 说说wcf中的那几种序列化
我们都知道wcf是由信道栈组成的,在我们传输的参数走到传输信道层之前,先需要经过序列化的过程,也就是将参数序列化为message,这篇 我们就来说说这里的序列化,蛮有意思的,可能初学者也明白,在wcf ...
- WCF技术剖析之十二:数据契约(Data Contract)和数据契约序列化器(DataContractSerializer)
原文:WCF技术剖析之十二:数据契约(Data Contract)和数据契约序列化器(DataContractSerializer) [爱心链接:拯救一个25岁身患急性白血病的女孩[内有苏州电视台经济 ...
随机推荐
- 【kotlin】报错:required:LIst<XXX> found:List<Unit>此类型的问题
出现问题如下: 解决方式如下: 解决思路:上面报出来的错误很明显,就是说想要的是List<XXX>类型但是给的却是List<Unit>类型,给的不是它想要的嘛 关键就是解决问题 ...
- 从头认识java-15.5 使用LinkedHashSet须要注意的地方
再接着上一个章节.我们来聊一下使用LinkedHashSet须要注意的地方. LinkedHashSet特点: (1)元素是有顺序的 (2)元素是不反复的 (3)底层数据结构是依照链表的结构存储的 ( ...
- Android安全机制介绍
Android的安全机制包含下面几个方面: • 进程沙箱隔离机制. • 应用程序签名机制. • 权限声明机制. • 訪问控制机制. • 进程通信机制. ...
- Solidworks如何绘制装饰螺纹线
1 插入-注解,装饰螺纹线 2 绘制装饰螺纹线,选择螺纹的边线,标准选择ISO,下面可以选择的范围就确定了(M6的孔,只能选择M8的螺纹或者M10的螺纹),画好之后在3D图中并没有明确的螺纹样式 ...
- sort-list——链表、快慢指针找中间、归并排序
Sort a linked list in O(n log n) time using constant space complexity. 链表,快慢指针找中点,归并排序. 注意判断条件fast-& ...
- ngnix
nginx的平滑重启 博客分类: nginx nginx平滑重启 在研发过程中,修改nginx的配置文件nginx.conf是很平常的事,需要重启nginx.如果我们直接reload是有一定风险的, ...
- 双向队列(STL做法)
双向队列 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 想想双向链表--双向队列的定义差点儿相同,也就是说一个队列的队尾同一 ...
- 一起来当网管(一)——Windows Server上的DHCP配置
学校实验室里大大小小设备还不少,网络环境虽说不复杂,但也比家用的复杂一些.就当练练手吧,刚好写点文章,免得以后实验室网络没人管了.那么就先从DHCP的配置来讲吧! 1.DHCP是什么.有什么用 DHC ...
- 凝视转换(c转换为c++)
C语言凝视->C++凝视即/*xxxxx*/->//xxxxx 在转换凝视前我们先了解一个概念:什么是有限状态机? 有限状态机FSM是软件上经常使用的一种处理方法,它把复杂的控制逻辑分解成 ...
- Devices下设备的进程显示为问号的问题
adb shell getprop ro.debuggable 返回 ro.debuggable=0 说明这个机子的版本不是userdebug版本,是user版本 只有这 ...