ASP.net在gridview中刪除資料時同時更新xml檔案

來源:互聯網
上載者:User
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>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.