本節通過一個實例介紹如何在程序中刪除XML文件中的節點。實例運行結果如圖1所示。
程序開發步驟:
(1)新建一個網站,其主頁默認為Default.aspx。
(2)在Default.aspx頁面中添加一個Xml控件,用來顯示XML文件中的內容,然後添加一個TextBox控件和一個Button控件,分別用來輸入要刪除的XML節點名和執行刪除操作。
(3)程序主要代碼如下。
當單擊【刪除】按鈕時,程序首先判斷TextBox1文本框中內容是否為空,如果不為空,則根據TextBox1文本框中內容在XML文件中找到對應節點,並通過XmlElement類的RemoveChild方法將該節點刪除,否則,彈出“請輸入要刪除的節點”信息提示框,並將網頁重新定向到該頁面。【刪除】按鈕的Click事件代碼如下:
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Trim() != "")
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("test.xml"));
XmlNodeList nodes;
XmlElement root = doc.DocumentElement;
nodes = root.SelectNodes("descendant::BOOK[TITLE='" + TextBox1.Text.Trim() + "']");
foreach (XmlNode node in nodes)
{
root.RemoveChild(node);
}
TextBox1.Text="";
Response.Write("<script>alert('刪除成功')</script>");
doc.Save(Server.MapPath("test.xml"));
XslTransform trans = new XslTransform();
trans.Load(Server.MapPath("test.xsl"));
Xml1.Document = doc;
Xml1.Transform = trans;
}
else
Response.Write("<script>alert('請輸入要刪除的節點');location='javascript:history.go(-1)';</script>");
}
完整程序代碼如下:
★ ★★★★Default.aspx頁面設計文件完整程序代碼★★★★★
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>刪除XML節點</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" border="1" cellpadding="0" cellspacing="0" style="width: 245px" bordercolor="#66cccc">
<tr><td style="font-size: 9pt; background-color: #66cccc; text-align: center;">
刪除XML節點</td></tr>
<tr>
<td style="font-size: 9pt; text-align: center; background-color: #ccffff;">
<asp:Xml ID="Xml1" runat="server"></asp:Xml></td>
</tr>
<tr>
<td style="text-align: center; font-size: 9pt; background-color: #ccffff;">
<asp:Label ID="Label1" runat="server" Font-Size="9pt" Text="書名:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Font-Size="9pt" Width="92px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="刪除" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
★ ★★★★Default.aspx.cs頁面代碼文件完整程序代碼★★★★★
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Xsl;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("test.xml"));
XslTransform trans = new XslTransform();
trans.Load(Server.MapPath("test.xsl"));
Xml1.Document = doc;
Xml1.Transform = trans;
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text.Trim() != "")
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("test.xml"));
XmlNodeList nodes;
XmlElement root = doc.DocumentElement;
nodes = root.SelectNodes(descendant::BOOK[TITLE=' + TextBox1.Text.Trim() + ']); foreach (XmlNode node in nodes) { root.RemoveChild(node); } TextBox1.Text = ; Response.Write(scriptalert('刪除成功')
nodes = root.SelectNodes("descendant::BOOK[TITLE='" + TextBox1.Text.Trim() + "']");
foreach (XmlNode node in nodes)
{
root.RemoveChild(node);
}
TextBox1.Text = "";
Response.Write("<script>alert('刪除成功')</script>");
doc.Save(Server.MapPath("test.xml"));
XslTransform trans = new XslTransform();
trans.Load(Server.MapPath("test.xsl"));
Xml1.Document = doc;
Xml1.Transform = trans;
}
else
{
Response.Write("<script>alert('請輸入要刪除的節點');</script>");
}
}
}
★ ★★★★test.xml XML文件完整程序代碼★★★★★
<?xml version="1.0" encoding="utf-8"?>
<PUBLICATION>
<BOOK>
<TITLE>C#數據庫系統開發完全手冊</TITLE>
<PAGES>628</PAGES>
</BOOK>
</PUBLICATION>
★ ★★★★test.xsl XML文件轉換文件完整程序代碼★★★★★
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<!--
This is an XSLT template file. Fill in this area with the
XSL elements which will transform your XML to XHTML.
-->
<table width="300" height="30" border="10" cellspacing="0" cellpadding="0" style="font-size: 9pt;">
<tr align="center">
<td style="font-size: 9pt;">書名</td>
<td style="font-size: 9pt;">頁碼</td>
</tr>
<xsl:for-each select="PUBLICATION/BOOK">
<tr align="center" height="30">
<td style="font-size: 9pt;">
<xsl:value-of select="TITLE"/>
</td>
<td style="font-size: 9pt;">
<xsl:value-of select="PAGES"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
圖1 刪除XML節點