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>");
}
}
}