xml文件格式
- <?xml version="1.0" encoding="utf-8"?>
- <messageList>
- <message id="1">
- <Name>熊猫</Name>
- <QQ>123456</QQ>
- <Email>[email protected]</Email>
- <Content>asfdsafasdfasdfsad我是内容</Content>
- <Time>2009-10-1</Time>
- </message>
- </messageList>
增加>>>>
- XmlDocument xmld = new XmlDocument();
- xmld.Load(Server.MapPath("App_Code/Message.xml"));
- XmlNode root = xmld.SelectSingleNode("messageList");
-
- int i = xmld.SelectSingleNode("messageList").ChildNodes.Count + 1;
- XmlElement xml0 = xmld.CreateElement("message");
- xml0.SetAttribute("id", i.ToString());
-
- XmlElement xml1 = xmld.CreateElement("Name");
- xml1.InnerText = this.txtName.Value.Trim();
- xml0.AppendChild(xml1);
-
- XmlElement xml2 = xmld.CreateElement("QQ");
- xml2.InnerText = this.txtQQ.Value.Trim();
- xml0.AppendChild(xml2);
-
- XmlElement xml3 = xmld.CreateElement("Email");
- xml3.InnerText = this.txtEmail.Value.Trim();
- xml0.AppendChild(xml3);
-
- XmlElement xml4 = xmld.CreateElement("Content");
- xml4.InnerText = this.txtContent.Value;
- xml0.AppendChild(xml4);
-
- XmlElement xml5 = xmld.CreateElement("Time");
- xml5.InnerText = DateTime.Now.ToString("yyyy-MM-dd");
- xml0.AppendChild(xml5);
-
- root.AppendChild(xml0);
-
- xmld.Save(Server.MapPath("App_Code/Message.xml"));
删除>>>>
- XmlDocument xmld = new XmlDocument();
-
- xmld.Load(Server.MapPath("App_Code/Message.xml"));
-
- XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;
-
- foreach (XmlNode var in list)
- {
- XmlElement xmle = (XmlElement)var;
-
- if (xmle.GetAttribute("id") == Id)
- {
- xmle.RemoveAll();
- xmld.Save(Server.MapPath("App_Code/Message.xml"));
- Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('删除成功!!');", true);
- }
- else
- {
- Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('删除失败!!');", true);
- }
- }
更改>>>>
- XmlDocument xmld = new XmlDocument();
- xmld.Load(Server.MapPath("App_Code/Message.xml"));
-
- XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;
-
- foreach (XmlNode xmln in list)
- {
- XmlElement xmle = (XmlElement)xmln;
- if (Request["ID"].Equals(xmle.GetAttribute("id")))
- {
- xmle.ChildNodes[0].InnerText = this.txtName.Value;
- xmle.ChildNodes[1].InnerText = this.txtQQ.Value;
- xmle.ChildNodes[2].InnerText = this.txtEmail.Value;
- xmle.ChildNodes[3].InnerText = this.txtContent.Value;
-
- xmld.Save(Server.MapPath("App_Code/Message.xml"));
- Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('修改成功!!!');location.href='MessageList.aspx'",true);
- }
- }
查询>>>>>
-
- XmlDocument xmld = new XmlDocument();
- xmld.Load(Server.MapPath("App_Code/Message.xml"));
-
- XmlNodeList list = xmld.SelectSingleNode("messageList").ChildNodes;
- if (list!=null)
- {
- foreach (XmlNode node in list)
- {
- XmlElement xmle = (XmlElement)node;
-
来自:http://blog.csdn.net/zhoukang0916/article/details/4684210
|
请发表评论