为了减少对数据库的访问,前台页面通常只对xml文件进行读取,但是更新数据库的时候需要同时更新xml文件,添加好办,但是删除的时候呢,下面的程序在gridview中删除数据的同时删除xml文件中对应的节点.xml文件的每个节点是一个图片新闻,包括图片和新闻页面的本地存储路径.
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.data.sqlclient;
using system.xml;
using system.io;
using system.text;
using system.configuration;
/**//// <summary>
/// pic_manage 的摘要说明
/// </summary>
public partial class pic_manage : system.web.ui.page
...{
public pic_manage()
...{
//
// todo: 在此处添加构造函数逻辑
//
}
protected void page_load(object sender, eventargs e)
...{
if (session["user"] == null || "admin" != (string)(session["user"]))
...{
response.redirect("login.aspx");
return;
}
}
protected void gridview1_rowdeleting(object sender, gridviewdeleteeventargs e)
...{
int key =convert.toint32( e.keys[0].tostring());
sqlconnection conn = new sqlconnection();
conn.connectionstring = configurationmanager.connectionstrings["ahpcconnectionstring"].connectionstring;
string strsql = "select url from [news] where news_id="+key;
sqldataadapter da = new sqldataadapter(strsql, conn);
dataset ds = new dataset();
da.fill(ds, "news");
string url = ds.tables[0].rows[0].itemarray[0].tostring();
conn.close();
url = url.split(new char[] ...{ '|' })[0];
string xmlpath = server.mappath("~/news/pic_news_list.xml");
xmldocument doc = new xmldocument();
doc.load(xmlpath);
xmlelement root = doc.documentelement;
xmlnodelist xnl = doc.selectsinglenode("ahpc").childnodes;
foreach (xmlnode xn in xnl)
...{
xmlelement xe = (xmlelement)xn;
if (xe.firstchild.lastchild.innertext==url) //因为在xml文件中只有url是各不相同的
...{
//删除该图片新闻中的所有图片
int count = xe.childnodes.count;
for (int i = 0; i < count - 3; i++)
...{
string delfile = "~/"+xe.childnodes[i].childnodes[1].innertext;
deletefile(delfile);
}
root.removechild(xe);//删除该节点的全部内容
}
}
doc.save(xmlpath);
//删除图片新闻网页文件
deletefile("~/news/picfile/" + url);
}
//删除文件函数
public void deletefile(string filepathname)
...{
try
...{
fileinfo delefile = new fileinfo(system.web.httpcontext.current.server.mappath(filepathname).tostring());
delefile.delete();
}
catch
...{
}
}
}
xml文件格式如下:
<?xml version="1.0" encoding="gb2312"?>
<ahpc>
<news>
<pic>
<title>3333</title>
<localurl>news/picfile/200704272014550.jpg</localurl>
<url>20070427201455.htm</url>
</pic>
<title>33333333333</title>
<abstract>《计算机组成原理及系统结构》课程设计指导书
课程编号:
课程名称(中文/英文):
计算机组成原理及系统结构/computer organization and architectur……</abstract>
<time>2007-4-27 20:14:55</time>
</news>
<news>
<pic>
<title>3333333333</title>
<localurl>news/picfile/200704272041170.jpg</localurl>
<url>20070427204117.htm</url>
</pic>
<pic>
<title>3333333333333333</title>
<localurl>news/picfile/200704272041171.jpg</localurl>
<url>20070427204117.htm</url>
</pic>
<title>44444444444444444444444444</title>
<abstract>三大大的三大幅阿三多发洒的发洒的发洒的……</abstract>
<time>2007-4-27 20:41:17</time>
</news>
<news>
<pic>
<title>22</title>
<localurl>news/picfile/200704281419150.jpg</localurl>
<url>20070428141915.htm</url>
</pic>
<title>333</title>
<abstract>3333333……</abstract>
<time>2007-4-28 14:19:15</time>
</news>
<news>
<pic>
<title>222</title>
<localurl>news/picfile/200704281520320.gif</localurl>
<url>20070428152032.htm</url>
</pic>
<title>333</title>
<abstract>33333333333333……</abstract>
<time>2007-4-28 15:20:32</time>
</news>
</ahpc>
新闻热点
疑难解答
图片精选