一、在页面中显示时,读缓存数据,加载xml数据
public void loaddata()
{
dataview source = (dataview)cache["mydata"];
if(source == null)
{
dataset ds = new dataset();
filestream fs = new filestream(server.mappath("loginmsg.xml"), filemode.open,fileaccess.read);
streamreader reader = new streamreader(fs);
ds.readxml(reader);
fs.close();
source = new dataview(ds.tables[0]);
cache.insert("mydata", source, new cachedependency(server.mappath("loginmsg.xml")));
errormsg.text = "从文件中读取的数据";
}
else
{
errormsg.text = "从缓存中读取的数据";
}
mydatagrid.datasource = source;
mydatagrid.databind();
}
二、用xml存储交互数据,将数据放置到缓存中
private void loginmsg_click(object sender, system.eventargs e)
{
if(!page.isvalid)
{
errormsg.text = "必需输入登录信息";
return;
}
dataset ds = new dataset();
filestream fs = new filestream(server.mappath("loginmsg.xml"),filemode.open, fileaccess.read, fileshare.readwrite);
streamreader reader = new streamreader(fs);
ds.readxml(reader);
fs.close();
datatable dt = ds.tables[0];
datarow newrow = dt.newrow();
newrow["custom_email"] = customeamil.text;
newrow["custom_pass"] = custompass.text;
newrow["login_time"] = datetime.now.tostring();
fs = new filestream(server.mappath("loginmsg.xml"), filemode.create, fileaccess.readwrite, fileshare.readwrite);
textwriter writer = new streamwriter(fs);
writer = textwriter.synchronized(writer);
ds.tables[0].rows.add(newrow);
ds.writexml(writer);
writer.close();
cache.remove("mydata");
loaddata();
}
新闻热点
疑难解答
图片精选