asp.net|xml|数据
为了减少对数据库的访问,前台页面通常只对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>