SerializeHelper
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization; namespace IOSerialize.Serialize
{
public class SerializeHelper
{
/// <summary>
/// 二进制序列化器
/// </summary>
public static void BinarySerialize()
{
//使用二进制序列化对象
string fileName = Path.Combine(Constant.SerializeDataPath, @"BinarySerialize.txt");//文件名称与路径
using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{//需要一个stream,这里是直接写入文件了
List<Programmer> pList = DataFactory.BuildProgrammerList();
BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器
binFormat.Serialize(fStream, pList);
}
using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{//需要一个stream,这里是来源于文件
BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器
//使用二进制反序列化对象
fStream.Position = ;//重置流位置
List<Programmer> pList = (List<Programmer>)binFormat.Deserialize(fStream);//反序列化对象
}
} /// <summary>
/// soap序列化器
/// </summary>
public static void SoapSerialize()
{
//使用Soap序列化对象
string fileName = Path.Combine(Constant.SerializeDataPath, @"SoapSerialize.txt");//文件名称与路径
using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
List<Programmer> pList = DataFactory.BuildProgrammerList();
SoapFormatter soapFormat = new SoapFormatter();//创建二进制序列化器
//soapFormat.Serialize(fStream, list);//SOAP不能序列化泛型对象
soapFormat.Serialize(fStream, pList.ToArray());
}
using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
SoapFormatter soapFormat = new SoapFormatter();//创建二进制序列化器
//使用二进制反序列化对象
fStream.Position = ;//重置流位置
List<Programmer> pList = ((Programmer[])soapFormat.Deserialize(fStream)).ToList();//反序列化对象
}
} //BinaryFormatter序列化自定义类的对象时,序列化之后的流中带有空字符,以致于无法反序列化,反序列化时总是报错“在分析完成之前就遇到流结尾”(已经调用了stream.Seek(0, SeekOrigin.Begin);)。
//改用XmlFormatter序列化之后,可见流中没有空字符,从而解决上述问题,但是要求类必须有无参数构造函数,而且各属性必须既能读又能写,即必须同时定义getter和setter,若只定义getter,则反序列化后的得到的各个属性的值都为null。 /// <summary>
/// XML序列化器
/// </summary>
public static void XmlSerialize()
{
//使用XML序列化对象
string fileName = Path.Combine(Constant.SerializeDataPath, @"Student.xml");//文件名称与路径
using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
List<Programmer> pList = DataFactory.BuildProgrammerList();
XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Programmer>));//创建XML序列化器,需要指定对象的类型
xmlFormat.Serialize(fStream, pList);
}
using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Programmer>));//创建XML序列化器,需要指定对象的类型
//使用XML反序列化对象
fStream.Position = ;//重置流位置
List<Programmer> pList = pList = (List<Programmer>)xmlFormat.Deserialize(fStream);
}
} /// <summary>
/// json也可以的
/// </summary>
public static void Json()
{
List<Programmer> pList = DataFactory.BuildProgrammerList();
string result = JsonHelper.ObjectToString<List<Programmer>>(pList);
List<Programmer> pList1 = JsonHelper.StringToObject<List<Programmer>>(result);
}
}
}
SerializeHelper的更多相关文章
- 数据导出至Excel文件--好库编程网http://code1.okbase.net/codefile/SerializeHelper.cs_2012122018724_118.htm
using System; using System.IO; using System.Data; using System.Collections; using System.Data.OleDb; ...
- C#设计模式之原型模式
原型模式:使用原型实例指定待创建对象的类型,并且通过复制这个原型来创建新的对象. 分析: 孙悟空:根据自己的形状复制(克隆)出多个身外身 软件开发:通过复制一个原型对象得到多个与原型对象一模一样的新对 ...
- 轻量级通信引擎StriveEngine —— C/S通信demo(2) —— 使用二进制协议 (附源码)
在网络上,交互的双方基于TCP或UDP进行通信,通信协议的格式通常分为两类:文本消息.二进制消息. 文本协议相对简单,通常使用一个特殊的标记符作为一个消息的结束. 二进制协议,通常是由消息头(Head ...
- 从零开始,搭建博客系统MVC5+EF6搭建框架(3),添加Nlog日志、缓存机制(MemoryCache、RedisCache)、创建控制器父类BaseController
一.回顾系统进度以及本章概要 目前博客系统已经数据库创建.以及依赖注入Autofac集成,接下来就是日志和缓存集成,这里日志用的是Nlog,其实还有其他的日志框架如log4,这些博客园都有很多介绍,这 ...
- DotNetBar for Windows Forms 11.8.0.8冰河之刃重打包版
关于 DotNetBar for Windows Forms 11.8.0.8_冰河之刃重打包版 基于 官方原版的安装包 + http://www.cnblogs.com/tracky 提供的补丁DL ...
- Asp.net Mvc 身份验证、异常处理、权限验证(拦截器)实现代码
本问主要介绍asp.net的身份验证机制及asp.net MVC拦截器在项目中的运用.现在让我们来模拟一个简单的流程:用户登录>权限验证>异常处理 1.用户登录 验证用户是否登录成功步骤直 ...
- C#编程总结(一)序列化
C#编程总结(一)序列化 序列化是将对象状态转换为可保持或传输的格式的过程.与序列化相对的是反序列化,它将流转换为对象.这两个过程结合起来,可以轻松地存储和传输数据. 几种序列化技术: 1) ...
- .net MVC简介、项目中每个文件夹的功能
MVC是微软2009对外公布的第一个开源的表示层框架,这是微软的第一个开源项目 M:viewmodel V:视图 c:控制器 App_Data:一个比较特殊的文件夹,把文件放到这个文件夹,通过地址 ...
- [Aaronyang]谈谈2015年AY对WPF全面技术总结40多篇WPF,炫到没朋友的AYUI来了
原著:AY WPF博客- 把wpf推广出去,让那些鄙视的人说不 大家好! 我是AY,首先声明,我在做一件很枯燥的事情,我是个91后程序员,每天熬夜完成计划的过着下班后的生活. 那天有 ...
随机推荐
- 让Windows XP的键盘“说话”,全语音键盘
长期面对无声的电脑,我们难免疲倦.如果正在输入的内容被系统一字(字母)不差地念出来,你还能在无声的疲倦中输入错误的内容吗?本文以Windows 2000/XP中一个鲜为人知的“讲述人”为例,来教你DI ...
- DevExpress控件使用方法:第二篇 barManager
标题栏 一.Bars 1. 把BarManager组件添加到窗体中后,会自动创建三个空的 bars: 主菜单(通常位于窗体顶部).顶部工具栏.窗体底部的状态栏. 2. 隐藏左侧的竖线和右边的箭 ...
- wampserver变橙色,apache 服务无法启动!问题解决小记(安装失败亦可参考)
http://blog.csdn.net/haoaiqian/article/details/58147079 80端口被占用的可能性非常大,关掉IIS即可
- npx:npm包执行器
npx 作用: 单次执行命令而不需要安装到本机 执行依赖包里的二进制文件 使用不同版本的 node 利用 npx 可以下载模块这个特点,可以指定某个版本的 Node 运行脚本.它的窍门就是使用 npm ...
- git push文件到远程github或者gitlab
Git global setup git config --global user.name "luozeng" git config --global user.email &q ...
- django配置setting文件
添加app到INSTALLED_APPS列表中 INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.co ...
- Centos维护命令
1.查看系统版本 cat /etc/issue cat /etc/redhat-release 3. cat /proc/version 4.uname -a 显示如下 5 uname -r (二)查 ...
- 电信版华为MATE7 EMUI4.0回退3.1和3.0教程与中转包
mate7升级6.0后遇到很多问题,想回退版本,找了很多教程,现在总结一下用中转包回退.EMUI4.0回退3.1,先下载B500中转包,将dload复制到2G以上内存卡根目录,不要三键强刷,会卡在开机 ...
- TL-WDR4310 v1 救砖
一.引出ttl线 用万用表测量,可以确定“GND”,第一个pin(焊盘为方的)为“Tx”,接下去依次为“Rx”.”GND”.”VCC“. 二.工具准备 下载tptpd软件工具,图标为 三.开始刷机 1 ...
- InfluxDB学习之InfluxDB的基本操作| Linux大学
来源地址:https://www.linuxdaxue.com/influxdb-study-series-manual.html 本文属于<InfluxDB系列教程>文章系列,该系列共包 ...