XML CODE:<?xml version="1.0" encoding="utf-8"?> <breakfast_menu> - <food><name>asdf</name><price>aasdf</price><description>asdf</description><calories>asdf</calories></food> - <food><name>Strawberry Belgian Waffles</name><price>$7.95</price><description>light Belgian waffles covered with strawberries and whipped cream</description><calories>900</calories></food> - <food><name>Berry-Berry Belgian Waffles</name><price>$8.95</price><description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description><calories>900</calories></food> - <food><name>French Toast</name><price>$4.50</price><description>thick slices made from our homemade sourdough bread</description><calories>600</calories></food> - <food><name>Homestyle Breakfast</name><price>$6.95</price><description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description><calories>950</calories></food> <food><name>CS从入门到精通</name><price>123</price><description>58.3</description></food> </breakfast_menu>
C# Code:XmlDocument doc = null; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { doc = new XmlDocument(); doc.Load(Server.MapPath("xmlfile.xml")); XmlNodeList list = doc.GetElementsByTagName("name"); foreach (XmlNode item in list) { DropDownList1.Items.Add(item.InnerText); } Session["msg"] = doc; } else { doc = Session["msg"] as XmlDocument; } } protected void sss() { XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("xmlfile.xml")); XmlNodeList list = doc.GetElementsByTagName("food"); foreach (XmlNode item in list) { DropDownList2.Items.Add(item.InnerText); } } protected void Button1_Click(object sender, EventArgs e) { XmlNode smlnode=doc.DocumentElement.SelectSingleNode("food[name='"+DropDownList1.SelectedItem.Text+"']"); TextBox1.Text = smlnode["name"].InnerText; TextBox2.Text = smlnode["price"].InnerText; TextBox3.Text = smlnode["description"].InnerText; TextBox4.Text = smlnode["calories"].InnerText; //这里是读取数据 } protected void Button2_Click(object sender, EventArgs e) { XmlNodeList node_num=doc.DocumentElement.SelectNodes("food[name='"+DropDownList1.SelectedItem.Text+"']"); int int1 = node_num.Count; int m = 0; foreach (XmlNode item in node_num) { m++; TextBox1.Text = item["name"].InnerText; } //这里是读取数据} protected void Button3_Click(object sender, EventArgs e) { //这里是读取数据XmlNode smlnode = doc.DocumentElement.SelectSingleNode("food[name='" + DropDownList1.SelectedItem.Text + "']"); foreach (XmlNode item in smlnode.ChildNodes) { if (item.LocalName=="name") { TextBox1.Text = item.InnerText; } if (item.LocalName == "price") { TextBox2.Text = item.InnerText; } if (item.LocalName == "description") { TextBox3.Text = item.InnerText; } if (item.LocalName == "calories") { TextBox4.Text = item.InnerText; } } } protected void Button4_Click(object sender, EventArgs e) { //这里是删除XmlNode smlnode = doc.DocumentElement.SelectSingleNode("food[name='" + DropDownList1.SelectedItem.Text + "']"); if (smlnode!=null) { doc.DocumentElement.RemoveChild(smlnode); DropDownList1.Items.RemoveAt(DropDownList1.SelectedIndex); doc.Save(Server.MapPath("456.xml")); } } protected void Button5_Click(object sender, EventArgs e) { //这里是修改XmlNode smlnode = doc.DocumentElement.SelectSingleNode("food[name='" + DropDownList1.SelectedItem.Text + "']"); foreach (XmlNode item in smlnode.ChildNodes) { if (item.LocalName == "name") { item.InnerText=TextBox1.Text; } if (item.LocalName == "price") { item.InnerText=TextBox2.Text; } if (item.LocalName == "description") { item.InnerText=TextBox3.Text; } if (item.LocalName == "calories") { item.InnerText = TextBox4.Text; } doc.Save(Server.MapPath("xmlfile.xml")); } } protected void Button6_Click(object sender, EventArgs e) { //这里是添加节点XmlNode smlnode = doc.DocumentElement.SelectSingleNode("food[name='" + DropDownList1.SelectedItem.Text + "']"); XmlNode root = doc.SelectSingleNode("breakfast_menu");//查找<bookstore> XmlElement xe1 = doc.CreateElement("food");//创建一个<book>节点 XmlElement xesub1 = doc.CreateElement("name"); xesub1.InnerText = "123"; xe1.AppendChild(xesub1);XmlElement xesub2 = doc.CreateElement("price"); xesub2.InnerText = "123"; xe1.AppendChild(xesub2); XmlElement xesub3 = doc.CreateElement("description"); xesub3.InnerText = "123"; xe1.AppendChild(xesub3); root.AppendChild(xe1); doc.Save(Server.MapPath("XMLFile.xml")); }