日期:2011-10-10  浏览次数:20332 次

为了减少对数据库的访问,前台页面通常只对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是各不相同的
            ...{

                //删除该图片新闻中的所有图片