.net中序列化和反序列化
string filePath = "data.xml";//定义文件名
DataSet ds = new DataSet();
if (!File.Exists(Server.MapPath(filePath)))
{
ds = 数据;//取数据
//序列化
BinaryFormatter bin = new BinaryFormatter();
StreamWriter dat = new StreamWriter(Server.MapPath(filePath));
bin.Serialize(dat.BaseStream, ds);
dat.Close();
}
else
{
//反序列化
BinaryFormatter bin = new BinaryFormatter();
StreamReader sr = new StreamReader(Server.MapPath(filePath));
ds = (DataSet)bin.Deserialize(sr.BaseStream);
sr.Close();
}
DataSet ds = new DataSet();
if (!File.Exists(Server.MapPath(filePath)))
{
ds = 数据;//取数据
//序列化
BinaryFormatter bin = new BinaryFormatter();
StreamWriter dat = new StreamWriter(Server.MapPath(filePath));
bin.Serialize(dat.BaseStream, ds);
dat.Close();
}
else
{
//反序列化
BinaryFormatter bin = new BinaryFormatter();
StreamReader sr = new StreamReader(Server.MapPath(filePath));
ds = (DataSet)bin.Deserialize(sr.BaseStream);
sr.Close();
}