首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 刪除 XML 節點實作(一):前端介面與刪除邏輯

     編輯:關於C語言

開發步驟

  1. 新建 ASP.NET 網站,預設頁面 Default.aspx。
  2. 頁面放置 Xml 控制項顯示資料,加入 TextBox(輸入書名)、Button(執行刪除)。
  3. 撰寫 Button1_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" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://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>
&nbsp;&nbsp;
<asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="刪除" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved