在昨天作业的基础上添加 :删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能(XmlDocument)。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace XML1
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        XmlDocument xdoc;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                xdoc = new XmlDocument();//声明一个XML文档。xdoc代表一个xml文档,但是代表谁现在没有定呢。
                xdoc.Load(Server.MapPath("books.xml"));//把books这个xml文档copy一份给xdco。

                XmlNodeList list = xdoc.GetElementsByTagName("name");

                foreach (XmlNode node in list)
                {
                    this.DropDownList1.Items.Add(node.InnerText);

                }
                Session["doc"] = xdoc;
            }
            else
            {
                xdoc = Session["doc"] as XmlDocument;
            }


        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            XmlNode node = xdoc.DocumentElement.SelectSingleNode("book[name='" + this.DropDownList1.Text + "']");

            #region 方法一
            //   foreach (XmlNode item in node.ChildNodes)
            //   {

            //       if (item.LocalName == "author")
            //       {
            //           this.txtAuthor.Text = item.InnerText;
            //       }
            //       if (item.LocalName == "publisher")
            //       {
            //           this.txtPublisher.Text = item.InnerText;
            //       }

            //       if (item.LocalName == "date")
            //       {
            //           this.txtDate.Text = item.InnerText;
            //       }

            //       if (item.LocalName == "isbn")
            //       {
            //           this.txtIsbn.Text = item.InnerText;
            //       }


            //       if (item.LocalName == "price")
            //       {
            //           this.txtPrice.Text = item.InnerText;
            //       }

 


            //   }


            #endregion


            #region 方法2
            txtAuthor.Text = node.SelectSingleNode("author").InnerText;
            txtPublisher.Text = node.SelectSingleNode("publisher").InnerText;
            txtDate.Text = node.SelectSingleNode("date").InnerText;
            txtIsbn.Text = node.SelectSingleNode("isbn").InnerText;
            txtPrice.Text = node.SelectSingleNode("price").InnerText;

            #endregion

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            XmlNode node=xdoc.DocumentElement.SelectSingleNode("book[name='" + this.DropDownList1.Text + "']");//找到要删除的节点
            xdoc.DocumentElement.RemoveChild(node);//把该节点在xmldocument实例中移除。
            this.DropDownList1.Items.RemoveAt(this.DropDownList1.SelectedIndex);//从下拉列表中把该项移除。
            xdoc.Save(Server.MapPath("books_new.xml"));
            this.Response.Write("删除成功");


        }

       

        protected void btnSelecte_Click1(object sender, EventArgs e)
        {
            XmlNode node = xdoc.DocumentElement.SelectSingleNode("book[name='" + this.DropDownList1.Text + "']");
            node.SelectSingleNode("author").InnerText = this.txtAuthor.Text;
            node.SelectSingleNode("publisher").InnerText = this.txtPublisher.Text;
            node.SelectSingleNode("date").InnerText = this.txtDate.Text;
            node.SelectSingleNode("isbn").InnerText = this.txtIsbn.Text;
            node.SelectSingleNode("price").InnerText = this.txtPrice.Text;

            xdoc.Save(Server.MapPath("books.xml"));
            Page.RegisterClientScriptBlock("key1", "<script type='text/javascript'>alert('save ok!')</script>");
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            XmlElement book= xdoc.CreateElement("book");
            XmlElement au= xdoc.CreateElement("author");
            au.AppendChild(xdoc.CreateTextNode(this.txtAuthor.Text));


            XmlElement pu = xdoc.CreateElement("publisher");
            pu.AppendChild(xdoc.CreateTextNode(this.txtPublisher.Text));
            XmlElement pr = xdoc.CreateElement("price");
            pr.AppendChild(xdoc.CreateTextNode(this.txtPrice.Text));
            XmlElement da = xdoc.CreateElement("date");
            da.AppendChild(xdoc.CreateTextNode(this.txtDate.Text));
            XmlElement isbn = xdoc.CreateElement("isbn");
            isbn.AppendChild(xdoc.CreateTextNode(this.txtIsbn.Text));
               
            XmlElement na = xdoc.CreateElement("name");
            na.AppendChild(xdoc.CreateTextNode(this.txtBookName.Text));
            book.AppendChild(au);
            book.AppendChild(pu);
            book.AppendChild(pr);
            book.AppendChild(da);
            book.AppendChild(isbn);
            book.AppendChild(na);
            xdoc.DocumentElement.AppendChild(book);
            xdoc.Save(Server.MapPath("books_new2.xml"));

            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "kk", "<script type='text/javascript'>alert('append ok!')</script>");

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值