在昨天作业的基础上添加 :删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能/新建页面DataXML,使用GridView显示book.xm中的图书信息

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


 
 


 


 

 

 

 

  Height="28px" Width="82px" />
 
Height="28px" Width="47px" />

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 作者:

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 

 

 
 
 
 

 



 
后台:
 
namespace SML21
{
 public partial class Demo3 : 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一份给xdoc
 

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按钮并完成对应的功能(如视频所示)
 
前台:
 


 


 


 

 

Xonrowdeleting="GridView1_RowDeleting"
Xonselectedindexchanging="GridView1_SelectedIndexChanging"
AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
GridLines="None" >
 
 
 
 
 
 
 
 
 
 
 

 
 
 
 
 
 
 
 
 
 
 



 


 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 作者:

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 
 

 

 
 

 


 


Xοnclick="Button4_Click" />
 

 

 
 
  Height="30px" Width="50px" />
 

 

 
  Width="104px" Xοnclick="Button6_Click" />
 


 



 
后台:
 
namespace DataListSetAndXml
{
 public partial class Demo1 : System.Web.UI.Page
 {
 
DataSet ds;
 protected void Page_Load(object sender, EventArgs e)
 {
 if (!IsPostBack)
 {
 ds = new DataSet();
 ds.ReadXml(Server.MapPath("books.xml")); //读出数据
 Session["ds"] = ds;
 bindXML();
 }
 else
{
 ds = Session["ds"] as DataSet;
 }
 }
 
private void bindXML()
 {
 this.GridView1.DataSource = ds.Tables[0].DefaultView;
 this.GridView1.DataBind();
 }
 
protected void Button4_Click(object sender, EventArgs e)
 {
 //添加
DataRow row =ds.Tables[0].NewRow();
 row["name"] = this.TextBox6.Text;
 row["author"] = this.TextBox1.Text;
 row["publisher"] = this.TextBox2.Text;
 row["date"] = this.TextBox3.Text;
 row["isbn"] = this.TextBox4.Text;
 row["price"] = this.TextBox5.Text;
 ds.Tables[0].Rows.Add(row);
 this.bindXML();
 }
 
protected void Button5_Click(object sender, EventArgs e)
 {//查找
 ds.Tables[0].DefaultView.RowFilter = "name like '%" + this.TextBox7.Text + "%'";//这个写法和sql语句中的where 条件一样。
 this.bindXML();
 }
 
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
 {//删除
 //1.在视图里完成删除
 //ds.Tables[0].DefaultView.Delete(e.RowIndex);
 //2.直接在DataTable中删除
 ds.Tables[0].Rows.RemoveAt(e.RowIndex);
 this.bindXML();//删完了进行再次绑定
 }
 protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
 {//编辑
 DataRowView row= ds.Tables[0].DefaultView[e.NewSelectedIndex];
 this.TextBox6.Text=row["name"].ToString();
 this.TextBox1.Text = row["author"].ToString();
 this.TextBox2.Text = row["publisher"].ToString();
 this.TextBox3.Text = row["date"].ToString();
 this.TextBox4.Text = row["isbn"].ToString();
 this.TextBox5.Text = row["price"].ToString();
 
}
 
protected void Button3_Click(object sender, EventArgs e)
 {//修改并保存
 int index= this.GridView1.SelectedIndex;
 DataRowView row= ds.Tables[0].DefaultView[index];
 row["name"]= this.TextBox6.Text;
 row["author"]= this.TextBox1.Text ;
 row["publisher"]= this.TextBox2.Text;
 row["date"]= this.TextBox3.Text;
 row["isbn"] = this.TextBox4.Text;
 row["price"] =this.TextBox5.Text ;
 
this.bindXML();
 }
 
protected void Button6_Click(object sender, EventArgs e)
 {//写入数据 生成xml文档
 ds.WriteXml(Server.MapPath("books2.xml"), XmlWriteMode.IgnoreSchema);
 ds.WriteXmlSchema(Server.MapPath("shema2.xml"));
 this.Response.Write("over!");
 
}
 

 

}
}

你可能感兴趣的:(在昨天作业的基础上添加 :删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能/新建页面DataXML,使用GridView显示book.xm中的图书信息)