asp.net update XML files while deleting data in the GridView

Source: Internet
Author: User
asp.net|xml| data

In order to reduce access to the database, the foreground page usually reads only the XML file, but when updating the database, you need to update the XML file at the same time, add a good one, but delete it, The following program deletes the data in the GridView and deletes the corresponding node in the XML file. Each node of the XML file is a picture news, including a local storage path for pictures and news pages.

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>
Summary description of Pic_manage
</summary>
public partial class Pic_manage:System.Web.UI.Page
... {
Public Pic_manage ()
... {
//
TODO: Add constructor logic here
//
}
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)//Because only URLs in the XML file are different
... {

                //Delete all pictures in the picture news
                 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);//Delete all contents of the node

}

}

Doc. Save (Xmlpath);

Delete Picture News page file
DeleteFile ("~/news/picfile/" + URL);

}

Delete a file function
public void DeleteFile (string filepathname)
... {
Try
... {
FileInfo delefile = new FileInfo (System.Web.HttpContext.Current.Server.MapPath (filepathname). ToString ());
Delefile.delete ();
}
Catch
... {
}
}


}

The XML file format is as follows:

<?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 composition principle and system structure" course design instruction Book
Course Number:
Course Name (Chinese/English):
Computer composition principle and system structure/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> three big Three big three showers of spilled hair ......</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>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.