.NET操作XML文件---[删除]
接上一遍博客------.NET操作XML文件---[修改]
删除的效果图:
删除前:
删除后:
deleteXml.aspx的代码:
using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Xml;//用于操作XMLpublic partial class deleteXml : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { bool flag=DeletePizza(); if (flag) { //若删除成功则跳转 Response.Redirect("readXml.aspx"); } } protected bool DeletePizza() { //获取url中的id字段 string id = Request.QueryString["id"].Trim(); //创建XML文件对象的实例doc XmlDocument doc = new XmlDocument(); //加载XML文件 doc.Load(HttpContext.Current.Server.MapPath("XMLFile.xml")); //查找对应id的结点 XmlNode thisNode = doc.DocumentElement.SelectSingleNode("/Pizza/Pizzas[@id=" + id + "]"); if (thisNode != null) { //删除该结点 thisNode.ParentNode.RemoveChild(thisNode); } //保存XML文件 doc.Save(HttpContext.Current.Server.MapPath("XMLFile.xml")); return true; }}