XML增、删、改和查示例    

前台代码:html
<%@ Page language="c#" Codebehind="Main.aspx.cs" AutoEventWireup="false" Inherits="DsAndXML.OpXMLFile.Main" %>


   
        Main
       
       
       
        http://schemas.microsoft.com/intellisense/ie5">
   
   
       
           
               
                新邮件地址:
                邮件地址:
               
               
               
               
               
                姓名:
               
               

       


   

XML文件dbGuest.xml


 
    aaa
    shanghai
    aaa@263.net
    ok
    2004-07-12T00:00:00.0000000+08:00
 

 
    shaoazhd
    beijing
    sss@22.net
    afsa
    2004-7-12 15:07:39
 

 
    Guset
    上海
    sfaf@22.net
 

 
    Guset
    上海
    ss@22.net
 

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.Xml;
using System.Xml.XPath;
namespace DsAndXML.OpXMLFile
{
    /**////


    /// Main 的摘要说明。
    ///

    public class Main : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Button btnQuery;
        protected System.Web.UI.WebControls.Button btnChange;
        protected System.Web.UI.WebControls.Button btnDelete;
        protected System.Web.UI.WebControls.Button btnAdd;
        protected System.Web.UI.WebControls.DropDownList ddlName;
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.Label Label2;
        protected System.Web.UI.WebControls.Label lbEmail;
        protected System.Web.UI.WebControls.TextBox tbNewMail;
        protected System.Web.UI.WebControls.Label Label3;
        protected System.Web.UI.WebControls.DataGrid dgShow;
   
        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if(!IsPostBack)
            Bind();

        }
        private void Bind()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(Server.MapPath(".\\db\\dbGuest.xml"));
            dgShow.DataSource = ds.Tables[0].DefaultView;
            dgShow.DataBind();
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
            XmlNodeList elemList = doc.GetElementsByTagName("Name");
            ddlName.Items.Clear();
            for(int i=0;i             ddlName.Items.Add(elemList[i].InnerXml);
           
        }

        Web Form Designer generated code#region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
       
        /**////


        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        ///

        private void InitializeComponent()
        {   
            this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
            this.btnChange.Click += new System.EventHandler(this.btnChange_Click);
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void btnQuery_Click(object sender, System.EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
            lbEmail.Text = doc.SelectSingleNode("//User[Name='"+ddlName.SelectedItem.Text+"']").ChildNodes.Item(2).InnerText;
        
        }

        private void btnChange_Click(object sender, System.EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
            XmlNodeList nodeList=xmlDoc.SelectSingleNode("dbGuest").ChildNodes;//获取dbGuest节点的所有子节点
            foreach(XmlNode xn in nodeList)//遍历所有子节点
            {
                XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
           
                XmlNodeList node = xe.GetElementsByTagName("Name");
                if(node.Count>0)
                {

                    if(node[0].InnerText==ddlName.SelectedItem.Text)
                    {
                        XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
                        foreach(XmlNode xn1 in nls)//遍历
                        {
                            XmlElement xe2=(XmlElement)xn1;//转换类型
                            if(xe2.Name=="Email")//如果找到
                            {
                                xe2.InnerText=tbNewMail.Text;//则修改
                                break;//找到退出来就可以了
                            }
                        }
                        break;
                    }
                }
               
            }
            xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
            Bind();
        }

        private void btnDelete_Click(object sender, System.EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
            XmlNodeList xnl=xmlDoc.SelectSingleNode("dbGuest").ChildNodes;
 
            foreach(XmlNode xn in xnl)
            {
                XmlElement xe=(XmlElement)xn;
                XmlNodeList node = xe.GetElementsByTagName("Name");
                if(node.Count>0)
                {

                    if(node[0].InnerText==ddlName.SelectedItem.Text)
                        xe.RemoveAll();//删除该节点的全部内容
                    break;
                }
            }
           
            xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
            Bind();
        }

        private void btnAdd_Click(object sender, System.EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath(".\\db\\dbGuest.xml"));
            XmlNode root=xmlDoc.SelectSingleNode("dbGuest");//查找
            XmlElement xe1=xmlDoc.CreateElement("User");//创建一个节点
            XmlElement xesub1=xmlDoc.CreateElement("Name");
            xesub1.InnerText="Guset";//设置文本节点
            xe1.AppendChild(xesub1);//添加到节点中
            XmlElement xesub2=xmlDoc.CreateElement("City");
            xesub2.InnerText="上海";
            xe1.AppendChild(xesub2);
            XmlElement xesub3=xmlDoc.CreateElement("Email");
            xesub3.InnerText="ss@22.net";
            xe1.AppendChild(xesub3);
 
            root.AppendChild(xe1);//添加到节点中
            xmlDoc.Save(Server.MapPath(".\\db\\dbGuest.xml"));
            Bind();
        }
    }
}

关联文档