当前路径:DAL/xml.cs using System; using System.Collections.Generic; using System.Text; using System.Configuration ; using System.Collections; using System.Xml; using System.Xml.XPath; using IDAL; using Model; namespace DAL { class xml:Iguestbook { string xmlGuestBookPath = System.Web.HttpContext.Current.Server.MapPath("../App_Data/") + ConfigurationSettings.AppSettings["xmlConnectionString"]; //得到所有留言 public guestbook[] getListGuestBook() { XmlDataDocument xmlDoc = new XmlDataDocument(); xmlDoc.Load(xmlGuestBookPath);//打开留言本xml文档 XmlNodeList xnl = xmlDoc.SelectSingleNode("guestBooks").ChildNodes;//读取所有结点 XmlElement xe ;//声明一个元素 ArrayList guest = new ArrayList(); foreach (XmlNode xn in xnl) { guestbook gb = new guestbook(); xe = (XmlElement)xn;//转换这个节点为一个元素 gb.Time = DateTime.Parse(xe.GetAttribute("time"));//得到留言时间 XmlNodeList xnlChild = xn.ChildNodes ; gb.Id = gb.Time.ToString();//以时间为主键 gb.Name = xn.ChildNodes[0].InnerText; gb.Ip = xn.ChildNodes[1].InnerText; gb.Content = xn.ChildNodes[2].InnerText; gb.Recontent = xn.ChildNodes[3].InnerText; gb.Pic = xn.ChildNodes[4].InnerText; gb.Email = xn.ChildNodes[5].InnerText; guest.Add(gb); } guest.Reverse();//数组反转,最后一条留言在第一位显示 return (guestbook[])guest.ToArray(typeof(guestbook)); } //得到一条留言 public guestbook[] getOnly(string id) { guestbook gb = new guestbook(); ArrayList guest = new ArrayList(); XmlDataDocument xmlDoc = new XmlDataDocument(); xmlDoc.Load(xmlGuestBookPath); XmlNodeList xnl = xmlDoc.SelectSingleNode("guestBooks").ChildNodes; XmlElement xe; foreach (XmlNode xn in xnl) { xe = (XmlElement)xn; gb.Time = DateTime.Parse (xe.GetAttribute("time")); gb.Id = gb.Time.ToString();//以时间为主键 if (xe.GetAttribute("time") == id) { XmlNodeList xnlChild = xn.ChildNodes; gb.Name = xn.ChildNodes[0].InnerText; gb.Ip = xn.ChildNodes[1].InnerText; gb.Content = xn.ChildNodes[2].InnerText; gb.Recontent = xn.ChildNodes[3].InnerText; gb.Pic = xn.ChildNodes[4].InnerText; gb.Email = xn.ChildNodes[5].InnerText; guest.Add(gb); } } return (guestbook[])guest.ToArray(typeof(guestbook)); } //添加留言内容 public void setGuestBook(guestbook gb) { XmlDataDocument xmlDoc = new XmlDataDocument(); xmlDoc.Load(this.xmlGuestBookPath); //选择根结点 XmlNode root = xmlDoc.SelectSingleNode("guestBooks"); XmlElement xe = xmlDoc.CreateElement("guestBook");//添加一个新结点元素 xe.SetAttribute("time", gb.Time.ToString());//添加元素属性 gb.Id = gb.Time.ToString();//以时间为关键字段 XmlElement xesub1 = xmlDoc.CreateElement("name");//创建子无素name xesub1.InnerText = gb.Name; xe.AppendChild(xesub1);//添加到xe结点中 XmlElement xesub2 = xmlDoc.CreateElement("ip"); xesub2.InnerText = gb.Ip; xe.AppendChild(xesub2); XmlElement xesub3 = xmlDoc.CreateElement("content"); xesub3.InnerText = gb.Content; xe.AppendChild(xesub3); XmlElement xesub4 = xmlDoc.CreateElement("recontent"); xesub4.InnerText = gb.Recontent; xe.AppendChild(xesub4); XmlElement xesub5 = xmlDoc.CreateElement("pic"); xesub5.InnerText = gb.Pic; xe.AppendChild(xesub5); XmlElement xesub6 = xmlDoc.CreateElement("email"); xesub6.InnerText = gb.Email; xe.AppendChild(xesub6); root.AppendChild(xe);//新建节点添加到根结点中 xmlDoc.Save(this.xmlGuestBookPath);//保存 } //回复留言 public void setReGuestBook(guestbook Regb) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(this.xmlGuestBookPath); XmlNodeList xnl = xmlDoc.SelectSingleNode("guestBooks").ChildNodes; foreach (XmlNode xn in xnl) { XmlElement xe = (XmlElement)xn; if (xe.GetAttribute("time") == Regb.Id) { xe.ChildNodes[3].InnerText = Regb.Recontent;//添加回复内容 } } xmlDoc.Save(this.xmlGuestBookPath); } //删除留言 public bool delGuestBook(string time) { XmlDataDocument xmlDoc = new XmlDataDocument(); xmlDoc.Load(this.xmlGuestBookPath); XmlNodeList xnl = xmlDoc.SelectSingleNode("guestBooks").ChildNodes ; XmlNode xnroot = xmlDoc.SelectSingleNode("guestBooks");//查找根结点,为删除子结点做准备 foreach (XmlNode xn in xnl)//循环所有子结点 { XmlElement xe = (XmlElement)xn; if (xe.GetAttribute("time") == time) //对比是否是要删除的结点 { xnroot.RemoveChild(xe);//从根结点删除 xmlDoc.Save(this.xmlGuestBookPath); return true; } } return false; } } }
相关源码
- HTML5响应式在线教育培训类企业商学院整站系统2020-10-21
- 图书管理系统2020-10-17
- 开单大师学习版2020-09-02
- 花语花店销售网站源码2020-08-03
- LONE网上购书源码2020-08-01
- 开单大师学习版源码2020-07-21
关于我们 | 顾问团队 | 发展历程 | 联系我们 | 源码上传
联系电话(Tel):4008-010-151(免长途) 企业QQ:2852517674
地址:北京市海淀区中关村鼎好大厦A座二层 邮编:100080
Room A-801,Dinghao Building,Zhongguancun,Beijing,China,100080
51Aspx.com 版权所有 CopyRight © 2006-2015. 京ICP备09089570号 | 京公网安备11010702000869号
联系电话(Tel):4008-010-151(免长途) 企业QQ:2852517674
地址:北京市海淀区中关村鼎好大厦A座二层 邮编:100080
Room A-801,Dinghao Building,Zhongguancun,Beijing,China,100080
51Aspx.com 版权所有 CopyRight © 2006-2015. 京ICP备09089570号 | 京公网安备11010702000869号