C#序列化和反序列化之一-二进制序列化
来源:互联网 发布:网红淘宝店前十名店铺 编辑:程序博客网 时间:2024/05/17 19:56
本系列文章主要介绍C#应用程序开发中常用的序列化和反序列化技术(不包含Web服务中的序列化,详见C#序列化与反序列化),包括以下两种方式:
- 二进制序列化
- XML序列化和反序列化
首先介绍二进制序列化,二进制序列化是使用BinaryFormatter 类的实例方法 Serialize(Stream, Object) 和 Deserialize(Stream) 方法来实现的,以下是本人写的一个支持泛型的BinarySerialize类:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Runtime.Serialization.Formatters.Binary;namespace JZC.Runtime.Serialization{ /// <summary> /// BinarySerialize class,provider basic Serialize and Deserialize method. /// </summary> public class BinarySerialize<T> { private string _strFilePath = string.Empty; public void Serialize(T obj, string strFilePath) { _strFilePath = strFilePath; FileInfo fi = new FileInfo(_strFilePath); if (fi.Exists) throw new ArgumentException("File specified is exist already!"); using (FileStream fs = new FileStream(_strFilePath, FileMode.Create)) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(fs, obj); } } /// <summary> /// Deserialize an instance of T. /// </summary> /// <typeparam name="T">Any type.</typeparam> /// <returns>The result of deserialized.</returns> public T DeSerialize(string filePath) { FileInfo fi = new FileInfo(filePath); if (!fi.Exists) throw new ArgumentException("File specified is not exist!"); T t; using (FileStream fs = new FileStream(filePath, FileMode.Open)) { BinaryFormatter formatter = new BinaryFormatter(); try { t = (T)formatter.Deserialize(fs); } catch (Exception ex) { throw ex; } } return t; } }}
假如我们有一个名字为Book的类:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace TestSerialize{ [Serializable] public class Book { public Book() { AllBookReaders = new ArrayList(); } public string BookName { get; set; } [NonSerialized()] public string strBookPwd; private string _bookID; public string BookID { get { return _bookID; } set { if (_bookID != value) { _bookID = value; } } } public ArrayList AllBookReaders { get; set; } private string _bookPrice; public string BookPrice { get { return _bookPrice; } set { if (_bookPrice != value) { _bookPrice = value; } } } }}
那么我们就可以用以下方法来序列化Book类,这里我使用了IList<Book>的对象来做测试。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using JZC.Runtime.Serialization;namespace TestSerialize{ class Program { static void Main(string[] args) { IList<Book> books = new List<Book>(); Book book = new Book(); book.BookID = "jzcBook1"; book.AllBookReaders.Add("jiangzhanchang"); book.AllBookReaders.Add("dingyinqian"); book.BookName = "C#Programming"; book.strBookPwd = "8907"; book.BookPrice = "69.00"; books.Add(book); book = new Book(); book.BookID = "JzcBook2"; book.AllBookReaders.Add("huanghaiyan"); book.AllBookReaders.Add("sunyouyi"); book.BookName = "WPF Primer"; book.strBookPwd = "0908"; book.BookPrice = "78.00"; books.Add(book); BinarySerialize<IList<Book>> serialize = new BinarySerialize<IList<Book>>(); string filePath=@"D:\bookSerialize.data"; serialize.Serialize(books, filePath); } }}这样,就会产生一个文件名为 D:\bookSerialize.data 的文件,我们可以在任何一个AppDomain中将这个文件反序列化成一个IList<Book>的对象,方法如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using JZC.Runtime.Serialization;using TestSerialize;namespace TestDeserialize{ class Program { static void Main(string[] args) { BinarySerialize<IList<Book>> serialize = new BinarySerialize<IList<Book>>(); IList<Book> books = serialize.DeSerialize(@"D:\bookSerialize.data"); } }}
我们可以加断点,看到反序列化的结果。
本文程序源代码下载
文章暂写到这里,本人写文章的功底还有待提升,请大家多多见谅。
参考文章列表:
- C#强化系列文章一:ViewState使用兼谈序列化
- C#序列化和反序列化
- C#序列化
待续......
- C#序列化和反序列化之一-二进制序列化
- C# 二进制、Xml、Json序列化和反序列化
- C# 二进制读写与序列化和反序列化
- C#序列化和反序列化之一
- c# 序列化和反序列化
- C#序列化和反序列化
- C#序列化和反序列化
- C#序列化和反序列化
- c#序列化和反序列化
- C#序列化和反序列化
- C#序列化和反序列化
- C# - 序列化和反序列化
- C#序列化和反序列化
- C#序列化和反序列化
- C#序列化和反序列化
- C#序列化和反序列化
- C#序列化和反序列化
- C# 序列化和反序列化
- SQL Server 没有日志文件(*.ldf) 只有数据文件(*.mdf) 恢复到SQL Server2005
- 3.20 Data class(纯稚的数据类)
- .net 获取上一周的时间段
- 《Linux那些事儿之我是USB》我是U盘(22)彼岸花的传说(一)
- 20111020学习总结
- C#序列化和反序列化之一-二进制序列化
- 软件测试
- 现在抛售苹果股票的都是2货
- 快速可扩展的Ajax流代理——提供持续下载跨域数据
- 软件工程,UML,开发模型和设计模式
- JavaScript学习-js中函数的重载
- 合理睡眠要点
- JAVA字节数据与JAVA类型的转换
- floyed求最小环