在昨天作業的基礎上添加 :刪除按鈕,修改並保存按鈕 和 添加按鈕。完成這些按鈕所對應的功能(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>");

        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章