📄 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;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -