📄 xmlpagecounter.cs
字号:
using System;
using System.Configuration;
using System.IO;
using System.Xml;
using System.Web;
namespace Common
{
/// <summary>
/// XmlPageCounter 的摘要说明。
/// </summary>
public class XmlPageCounter : IPageCounter
{
private string _filepath = string.Empty;
public XmlPageCounter()
{
//从配置文件中读取xml文件路径
_filepath = ConfigurationSettings.AppSettings["FilePath"];
_filepath = HttpContext.Current.Server.MapPath(_filepath);
//初始化XML文档
InitPageCounter(_filepath);
}
#region IPageCounter 成员
public int GetPageCount()
{
//加载XML文档
XmlDocument doc = new XmlDocument();
doc.Load(_filepath);
//定位PageCount节点
XmlNode node = doc.SelectSingleNode("/SiteStat/PageCount");
//获取计数器计数结果
return Convert.ToInt32(node.InnerText);
}
public void SetPageCount(int iCount)
{
//加载XML文档
XmlDocument doc = new XmlDocument();
doc.Load(_filepath);
//定位PageCount节点
XmlNode node = doc.SelectSingleNode("/SiteStat/PageCount");
//设置计数器值
node.InnerText = iCount.ToString();
//保存XML文档
doc.Save(_filepath);
}
public void IncreasePageCount()
{
//计数器结果加1
SetPageCount(GetPageCount() + 1);
}
#endregion
/// <summary>
/// 初始化计数器xml文件
/// </summary>
private void InitPageCounter(string strFilePath)
{
XmlDocument doc = new XmlDocument();
if (!File.Exists(strFilePath)) //如果制定的xml文件不存在
{
doc.LoadXml("<?xml version='1.0' ?><SiteStat><PageCount>" +
"0</PageCount></SiteStat>");
doc.Save(strFilePath);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -