将数据结构类型序列化和反序列化(BinaryFormatter类)
来源:百度文库 编辑:神马文学网 时间:2024/06/05 15:14:45
from http://www.cnblogs.com/tuyile006/archive/2006/12/28/605958.html简单例子如下:
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
加上命名空间
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//the struct to serializer
[Serializable]
public struct mydata
{
public string myname;
public string mybirthday;
public int age;
public byte[] imageByte;
}
//选择保存路径
private void button1_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "XML文件|*.xml";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
textBox1.Text=saveFileDialog1.FileName;
}
//将数据结构序列化到bat或其他格式文件中
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
return;
mydata newdata = new mydata();
newdata.myname = "龙域";
newdata.mybirthday = "09-31";
newdata.age = 23;
newdata.imageByte = Encoding.Default.GetBytes("999adfklasdjfl".ToCharArray());
IFormatter formatter = new BinaryFormatter();
FileStream fs = new FileStream(textBox1.Text, FileMode.OpenOrCreate);
formatter.Serialize(fs, newdata);
fs.Close();
}
//反序列化二进制文件
private void button3_Click(object sender, EventArgs e)
{
IFormatter formter = new BinaryFormatter();
FileStream fs = new FileStream(textBox1.Text,FileMode.Open);
mydata mm=(mydata)formter.Deserialize(fs);
MessageBox.Show("mm.Name="+mm.myname+"\r\n"+
"mm.bothday=" + mm.mybirthday + "\r\n" +
"mm.age="+mm.age+"\r\n"+
"mm.imageByte="+Encoding.Default.GetString(mm.imageByte));
fs.Close();
}
}
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
加上命名空间
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//the struct to serializer
[Serializable]
public struct mydata
{
public string myname;
public string mybirthday;
public int age;
public byte[] imageByte;
}
//选择保存路径
private void button1_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "XML文件|*.xml";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
textBox1.Text=saveFileDialog1.FileName;
}
//将数据结构序列化到bat或其他格式文件中
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
return;
mydata newdata = new mydata();
newdata.myname = "龙域";
newdata.mybirthday = "09-31";
newdata.age = 23;
newdata.imageByte = Encoding.Default.GetBytes("999adfklasdjfl".ToCharArray());
IFormatter formatter = new BinaryFormatter();
FileStream fs = new FileStream(textBox1.Text, FileMode.OpenOrCreate);
formatter.Serialize(fs, newdata);
fs.Close();
}
//反序列化二进制文件
private void button3_Click(object sender, EventArgs e)
{
IFormatter formter = new BinaryFormatter();
FileStream fs = new FileStream(textBox1.Text,FileMode.Open);
mydata mm=(mydata)formter.Deserialize(fs);
MessageBox.Show("mm.Name="+mm.myname+"\r\n"+
"mm.bothday=" + mm.mybirthday + "\r\n" +
"mm.age="+mm.age+"\r\n"+
"mm.imageByte="+Encoding.Default.GetString(mm.imageByte));
fs.Close();
}
}
将数据结构类型序列化和反序列化(BinaryFormatter类)
深入探讨C#序列化和反序列化
Web Service深度编程——Axis序列化/反序列化器开发指南
Web Service深度编程——Axis序列化/反序列化器开发指南
PHP 序列化(serialize)格式详解
排序(数据结构)
排序(数据结构)
时间序列和横截面
关于php序列化的问题
【精华】数据结构和算法版旧的精华贴 数据结构与算法 第二帖
蛋白序列可以和DNA序列不对应
数据结构与算法(C++版)
黄金比率和费波纳奇数列(上)
黄金比率和费波纳奇数列(上)
【少校简评】中国版“全球鹰”和“捕食者”将真实列入战斗序列_军情前沿-【陆战队少校】博报_...
IPVS代码阅读笔记(一):重要数据结构
数据结构排序
TIN数据结构
数据结构 课程设计
将long类型的值存入char类型数组里面
如何将excel 两列变成一列?***
战略类型和战略选择
汉字性质和文章类型
汉字性质和文字类型