序列化简单点来理解就是把内存的东西写到硬盘中,当然也可以写到内存中(这个内容我会在后面写一个例子).而反序列化就是从硬盘中把信息读到内存中.就这么简单,呵呵,现在来看下面的例子吧!
在这篇文章中我将使用BinaryFormatter序列化类Book作为例子,希望大家能从例子中深刻体会什么是序列化.
定义类Book:
[Serializable]
public class Book
{
string name;
float price;
string author;
public Book(string bookname, float bookprice, string bookauthor)
{
name = bookname;
price = bookprice;
author = bookauthor;
}
}
在类的上面增加了属性:Serializable.(如果不加这个属性,将抛出SerializationException异常).
通过这个属性将Book标志为可以序列化的.当然也有另一种方式使类Book可以序列化,那就是实行ISerializable接口了.在这里大家要注意了:Serializable属性是不能被继承的咯!!!
如果你不想序列化某个变量,该怎么处理呢?很简单,在其前面加上属性[NonSerialized] .比如我不想序列化
string author;
那我只需要
[NonSerialized]
string author;
好了,现在就告诉大家怎么实现序列化:
我们使用namespace:
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
首先创建Book实例,like this:
Book book = new Book("Day and Night", 30.0f, "Bruce");
接着当然要创建一个文件了,这个文件就是用来存放我们要序列化的信息了.
FileStream fs = new FileStream(@"C:/book.dat", FileMode.Create);
序列化的实现也很简单,like this:
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, book);
很简单吧!现在我列出整个原代码,包括反序列化.
static void Main(string[] args)
{
Book book = new Book("Day and Night", 30.0f, "Bruce");
using(FileStream fs = new FileStream(@"C:/book.dat", FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, book);
}
book = null;
using(FileStream fs = new FileStream(@"C:/book.dat", FileMode.Open)){
BinaryFormatter formatter = new BinaryFormatter();
book = (Book)formatter.Deserialize(fs);//在这里大家要注意咯,他的返回值是object
}
}
http://www.codeproject.com/KB/tree/loadandsave.aspx
利用串行化保存树内容:treenode实现了Iserialize接口
using System;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace jhTreeViewTools
{
/// <summary>
/// jhTreeViewTools is a namespace for classes around the TreeView control
/// This (only) one is a class for saving and loading the TreeView
/// </summary>
public class LoadAndSave
{
#region Save (saveTree, saveNode)
/// <summary>
/// Save the TreeView content
/// </summary>
/// <param name="tree"></param>
/// <param name="filename"></param>
/// <returns>Errorcode as int</returns>
public static int saveTree(TreeView tree, string filename)
{
// Neues Array anlegen
ArrayList al = new ArrayList();
foreach (TreeNode tn in tree.Nodes) //这里node是根节点,但是序列化时候会保存它的子节点
{
// jede RootNode im TreeView sichern ...
al.Add(tn);
}
// Datei anlegen
Stream file = File.Open(filename, FileMode.Create);
// Bin鋜-Formatierer init.
BinaryFormatter bf = new BinaryFormatter();
try
{
// Serialisieren des Arrays
bf.Serialize(file, al);
}
catch (System.Runtime.Serialization.SerializationException e)
{
MessageBox.Show("Serialization failed : {0}", e.Message);
return -1; // ERROR
}
// Datei schliessen
file.Close();
return 0; // OKAY
}
#endregion
#region Load (loadTree, searchNode)
/// <summary>
/// Load the TreeView content
/// </summary>
/// <param name="tree"></param>
/// <param name="filename"></param>
/// <returns>Errorcode as int</returns>
public static int loadTree(TreeView tree, string filename)
{
if (File.Exists(filename))
{
// Datei 鰂fnen
Stream file = File.Open(filename, FileMode.Open);
// Bin鋜-Formatierer init.
BinaryFormatter bf = new BinaryFormatter();
// Object var. init.
object obj = null;
try
{
// Daten aus der Datei deserialisieren
obj = bf.Deserialize(file);
}
catch (System.Runtime.Serialization.SerializationException e)
{
MessageBox.Show("De-Serialization failed : {0}", e.Message);
return -1;
}
// Datei schliessen
file.Close();
// Neues Array erstellen
ArrayList nodeList = obj as ArrayList;
// load Root-Nodes
foreach (TreeNode node in nodeList)
{
tree.Nodes.Add(node);
}
return 0;
}
else return -2; // File existiert nicht
}
#endregion
}
}