1.设置xml文件 XMLFile.xml
<?xml version="1.0" encoding="utf-8"?>
<DocumentElement>
<Students>
<ID>1</ID>
<NAME>雅琴</NAME>
<Phone>0101010101</Phone>
<Address>沈阳新区乐闻街</Address>
<City>沈阳</City>
</Students>
<Students>
<ID>2</ID>
<NAME>浩宇</NAME>
<Phone>2323232323</Phone>
<Address>天津崇文门</Address>
<City>天津</City>
</Students>
<Students>
<ID>3</ID>
<NAME>乐凯</NAME>
<Phone>7878787878</Phone>
<Address>永安欣欣城</Address>
<City>上海</City>
</Students>
<Students>
<ID>4</ID>
<NAME>张余宁</NAME>
<Phone>15866668559</Phone>
<Address>徐家新城</Address>
<City>杭州</City>
</Students>
</DocumentElement>
2.新建网站-空网站
添加WEB窗体
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="xxx.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></title>
</head>
<body>
<form id="form1" runat="server">
<h3>修改和删除XML文档节点</h3>
<asp:GridView ID="GridView1" runat="server" BackColor="White"
BorderColor="#E7E7FF" BorderWidth="1px" CellPadding="3"
GridLines="Horizontal" BorderStyle="None">
<AlternatingRowStyle BackColor="#F7F7F7" />
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
请选择节点<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
</asp:DropDownList>
<asp:Button ID="Button2" runat="server" Text="删除"
οnclick="Button2_Click" />
<br />
新节点名<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1"
runat="server" Text="修改" οnclick="Button1_Click" />
</form>
</body>
</html>
后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
namespace xxx{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("XMLFile.xml"));
GridView1.DataSource = ds;
GridView1.DataBind();
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "NAME";
DropDownList1.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("XMLFile.xml"));
XmlNodeList node;
XmlElement root = doc.DocumentElement;
node = root.SelectNodes("descendant::basic[NAME='"+DropDownList1 .Text .Trim ()+"']");
foreach (XmlNode n in node)
{
root.RemoveChild(n);
}
Response.Write("<script>alert('删除节点成功!')</script>");
doc.Save(Server.MapPath("XMLFile.xml"));
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("XMLFile.xml"));
XmlNodeList xnl = doc.SelectSingleNode("DocumentElement").ChildNodes; ;//获取NewDataSet节点的所有子节点
foreach (XmlNode xn in xnl)//遍历所有子节点
{
XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
if (xe.Name == "Students")//判断节点名为Table
{
XmlNodeList xnlChild = xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach (XmlNode xnChild in xnlChild)//遍历
{
XmlElement xeChild = (XmlElement)xnChild;//转换类型
if (xeChild.Name == "NAME" && xeChild.InnerText == this.DropDownList1.SelectedValue.Trim())
{
xeChild.InnerText = TextBox1.Text.Trim();
Response.Write("<script>alert('修改成功')</script>");
}
}
}
}
doc.Save(Server.MapPath("XMLFile.xml"));
Response.Write("<script>location='Default.aspx'</script>");
}
}
}