您的位置:首页 > 编程语言 > ASP

简单ASP.NET + XML计数器

2005-07-15 18:30 323 查看
有时要想做一个纯粹的计数流量器,可以采用XML来替代数据库,速度特快.

counter.aspx

<%@ Page Language="C#" Debug="true" codepage="936"%>
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml" %>

<script language="c#" runat="server">
//*****************************************************
// 调用方法
// 计数: counter.aspx?count=yes
// 显示: counter.aspx
//*****************************************************

public string Exec = string.Empty;

private void Page_Load(object sender, System.EventArgs e)
{
string fileName = "counter.xml";
string filePath = Server.MapPath(fileName);
if ( File.Exists(filePath) )
{
Exec = Request.QueryString["count"];
if ( Exec != null && Exec != string.Empty )
{
XmlDocument xdoc = new XmlDocument();
try
{
xdoc.Load(filePath);
XmlNodeList xnl = xdoc.GetElementsByTagName("Datas");
XmlNode xn = xnl[0];

XmlNode SettingNode = xn.FirstChild;
int Counts = int.Parse(SettingNode.InnerText) + 1;
SettingNode.InnerText = Counts.ToString();
xdoc.Save(filePath);
}
finally
{
xdoc = null;
}
}
else
{
XmlDocument xdoc = new XmlDocument();
try
{
xdoc.Load(filePath);
XmlNodeList xnl = xdoc.GetElementsByTagName("Datas");
XmlNode xn = xnl[0];

XmlNode SettingNode = xn.FirstChild;
int Counts = int.Parse(SettingNode.InnerText);
Response.Write(Counts.ToString());
}
finally
{
xdoc = null;
}
}
}
else
Response.Write("NOT");
}
</script>

counter.xml

<?xml version="1.0" encoding="utf-8"?>
<DataList>
<Datas>
<Count>0</Count>
</Datas>
</DataList>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: