1. 在昨天作业的基础上添加 :删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能(XmlDocument)。
前台:
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+"']");
//方法一:
foreach (XmlNode item in node.ChildNodes)
{
if (item.LocalName == "author")
{
this.TextBox1.Text = item.InnerText;
}
if (item.LocalName == "publisher")
{
this.TextBox2.Text = item.InnerText;
}
if (item.LocalName == "date")
{
this.TextBox3.Text = item.InnerText;
}
if (item.LocalName == "isbn")
{
this.TextBox4.Text = item.InnerText;
}
if (item.LocalName == "price")
{
this.TextBox5.Text = item.InnerText;
}
}
//方法二:
TextBox1.Text = node.SelectSingleNode("author").InnerText;
TextBox2.Text = node.SelectSingleNode("publisher").InnerText;
TextBox3.Text = node.SelectSingleNode("date").InnerText;
TextBox4.Text = node.SelectSingleNode("isbn").InnerText;
TextBox5.Text = node.SelectSingleNode("price").InnerText;
}
protected void Button2_Click(object sender, EventArgs e)
{//删除
XmlNode node= xdoc.DocumentElement.SelectSingleNode("book[name'"+this.DropDownList1.Text+"']");//找到要删除的节点
xdoc.DocumentElement.RemoveChild(node);//把该节点在xml document实例中移除
this.DropDownList1.Items.RemoveAt(this.DropDownList1.SelectedIndex);
xdoc.Save(Server.MapPath("books_new.xml"));
this.Response.Write("删除");
}
protected void Button3_Click(object sender, EventArgs e)
{//修改并保存
XmlNode node = xdoc.DocumentElement.SelectSingleNode("book[name'" + this.DropDownList1.Text+"']");
node.SelectSingleNode("author").InnerText = this.TextBox1.Text;
node.SelectSingleNode("publisher").InnerText = this.TextBox2.Text;
node.SelectSingleNode("date").InnerText = this.TextBox3.Text;
node.SelectSingleNode("isbn").InnerText = this.TextBox4.Text;
node.SelectSingleNode("price").InnerText = this.TextBox5.Text;
xdoc.Save(Server.MapPath("books.xml"));
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key2", "");
// Page.RegisterClientScriptBlock("key1","");
}
protected void Button4_Click(object sender, EventArgs e)
{//添加
XmlElement book= xdoc.CreateElement("book");
XmlElement na = xdoc.CreateElement("name");
na.AppendChild(xdoc.CreateTextNode(this.TextBox6.Text));
XmlElement au = xdoc.CreateElement("author");
au.AppendChild(xdoc.CreateTextNode(this.TextBox1.Text));
XmlElement pu = xdoc.CreateElement("publisher");
au.AppendChild(xdoc.CreateTextNode(this.TextBox2.Text));
XmlElement da = xdoc.CreateElement("date");
au.AppendChild(xdoc.CreateTextNode(this.TextBox3.Text));
XmlElement isd = xdoc.CreateElement("isbn");
au.AppendChild(xdoc.CreateTextNode(this.TextBox4.Text));
XmlElement pr = xdoc.CreateElement("price");
au.AppendChild(xdoc.CreateTextNode(this.TextBox5.Text));
book.AppendChild(na);
book.AppendChild(au);
book.AppendChild(pu);
book.AppendChild(da);
book.AppendChild(isd);
book.AppendChild(pr);
xdoc.DocumentElement.AppendChild(book);
this.DropDownList1.Items.Add(this.TextBox6.Text);
xdoc.Save(Server.MapPath("books_new2.xml"));
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "kk", "");
}
}
}
2.新建页面DataXML,使用GridView显示book.xm中的图书信息(将books.xml文档和dataSet交互。注意:DataView的使用。ds.Tables[0].defaultView)。添加 删除、编辑、修改并保存、添加、查找、生成XML按钮并完成对应的功能(如视频所示)
前台:
}
}