/*
filename: savearrary2file.cs
author : zhanghua
date : 2005-8-27
funciton: 本实例介绍如何将数组序列化到文件中,
*/
using system;
using system.io;
using system.runtime.serialization;
using system.runtime.serialization.formatters.binary;
class class1
{
///<summary>
///应用程序的主入口点
///</summary>
[stathread]
static void main(string[] args)
{
//
//定义purchaseorder类型的数组对象obj, 设置其数组长度为2,
// 并在其中保存了用户要序列化的数据。
purchaseorder obj = new purchaseorder();
obj.itemsorders = new item[2];
obj.itemsorders[0] = new item();
obj.itemsorders[0].itemid = "first";
obj.itemsorders[0].itemprice = 500.25m;
obj.itemsorders[1] = new item();
obj.itemsorders[1].itemid = "second";
obj.itemsorders[0].itemprice = 66.88m;
//创建一个文件流对象stream,指向文件myfile.bin
iformatter formatter = new binaryformatter();
stream stream = new filestream("d://myfile.bin", filemode.create,
fileaccess.write, fileshare.none);
//通过formatter对象以二进制格式将obj对象序列化后到文件myfile.bin中
formatter.serialize(stream,obj);
stream.close();
}
//定义两个可序列化的类purchaseorder 和item
[serializable]
public class purchaseorder
{
public item [] itemsorders;
}
[serializable]
public class item
{
public string itemid;
public decimal itemprice;
}
}
/*
summary : 通过本实例可以学习到如何将比较复杂的结构序列化到文件中
*/
新闻热点
疑难解答