xml文件格式
- <?xml version="1.0" encoding="utf-8"?>
- <messageList>
- <message id="1">
- <Name>熊猫</Name>
- <QQ>123456</QQ>
- <Email>panda@qq.com</Email>
- <Content>asfdsafasdfasdfsad我是内容</Content>
- <Time>2009-10-1</Time>
- </message>
- </messageList>
增加>>>>
- XmlDocument xmld = new XmlDocument();
- xmld.Load(Server.MapPath("App_Code/Message.xml"));//加载xml文件
- XmlNode root = xmld.SelectSingleNode("messageList");//查找messageList节点
- int i = xmld.SelectSingleNode("messageList").ChildNodes.Count + 1;
- XmlElement xml0 = xmld.CreateElement("message");
- xml0.SetAttribute("id", i.ToString());
- XmlElement xml1 = xmld.CreateElement("Name");//创建一个Name节点
- xml1.InnerText = this.txtName.Value.Trim();
- xml0.AppendChild(xml1);//添加
- XmlElement xml2 = xmld.CreateElement("QQ");//创建QQ节点
- xml2.InnerText = this.txtQQ.Value.Trim();
- xml0.AppendChild(xml2);//添加
- XmlElement xml3 = xmld.CreateElement("Email");//创建一个Email节点
- xml3.InnerText = this.txtEmail.Value.Trim();
- xml0.AppendChild(xml3);//添加
- XmlElement xml4 = xmld.CreateElement("Content");//创建一个Content节点
- xml4.InnerText = this.txtContent.Value;
- xml0.AppendChild(xml4);//添加
- XmlElement xml5 = xmld.CreateElement("Time");//创建一个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)//如果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();//构建xml文档
- 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