站长中国
设为首页 | 站长论坛

站长论坛 站长下载
您所在的位置: 站长中国 > 站长学院 > 网络编程 > ASP专区 > 正文

ASP.NET: XML计数器第二版
  2007年09月12日18:18:58  评论(1条) 字体:[ ]
相关热点: the to navigator.Insert of TreePosition.FirstChild
Code:
1) counter.aspx :- The Counter Page

<%@ Import Namespace="System.IO" %>
<%@ Assembly Name="System.Xml" %>
<%@ Import Namespace="System.Xml" %>
<%@ page language="c#" EnableSessionState="True" %>
<%-- These are the imported assembiles and namespaces need to run the counter --%>
<html>
<head>
<title>Saurabh's XML Counter Script</title>
<script language="C#" runat="server">
//script is called when the page is loaded
public void Page_Load(Object src, EventArgs e)
{
//the path to the Xml file which will contain all the data
//modify this if you have any other file or directory mappings.
//modify this if you have been directed here from Step 2 of the ReadMe file.
string datafile="db/xmlcounter.xml" ;

if(!Page.IsPostBack){
//try-catch block containing the counter code
try {
//create an instance of the class XmlDocument
XmlDocument xmldocument = new XmlDocument() ;

//Open a FileStream to the specified file
FileStream fin ;
//It is very Important to specify the "FileShare.ReadWrite" option.
//This allows other viewers to also read and write to the Database
//This was missing in my last release hence there was a BUG !!!
fin = new FileStream(Server.MapPath(datafile), FileMode.Open, FileAccess.Read,
FileShare.ReadWrite) ;
//Load the Document
xmldocument.Load(new StreamReader(fin)) ;
fin.Close();
//create an instance of the DocumentNavigator class used to
//navigate through and XML file
DocumentNavigator navigator = new DocumentNavigator(xmldocument) ;

//Move to the first element (in my file 'Visitors')
navigator.MoveToDocumentElement() ;
//move to it child at position '0' (ie.in my file 'total' node)
navigator.MoveToChild(0) ;

//check if we are on the right element which has an attribute
if (navigator.HasAttributes) {
//get the attribute of the node 'total' called 'tot' (see the xmlcounter.xml file)
//since the value stored is in a string format we 'cast' it into a Int type
int total = int.Parse(navigator.GetAttribute("tot")) ;
//increase the counter
total++ ;
//show the counter on the page
countmess.Text = "You are visitor No: "+total.ToString() ;
//save the incremented counter back in the XML file
navigator.SetAttribute(0,total.ToString() );
}

//Update the Database only if a new session is there
if(Session["counter"]==null)
{
//move back to the Document element
navigator.MoveToDocumentElement() ;
navigator.MoveToChild(0) ;
//then insert the element after the 'total' element which will contain all
//the information of a single visitor
navigator.Insert(TreePosition.After , XmlNodeType.Element, "Viewer","","") ;
//make an instance to the HttpUrl class to get information of the referrer to
//the page if any. if there are no referrers then by Default this object is 'null'
//so we have to make a check if it is null and do the needful
HttpUrl objUrl = Request.UrlReferrer;
if(objUrl!=null)
{
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Element,"Referrer","","");
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Referrer","","") ;
navigator.Value = objUrl.Host ;
}
else
{
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Element,"Referrer","","");
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Referrer","","") ;
navigator.Value = "Direct" ;
}
//release the resource for Garbage collection
objUrl=null ;
//move to parent node and then insert the information about the useragent
navigator.MoveToParent() ;
navigator.Insert(TreePosition.After, XmlNodeType.Element,"UserAgent","","" ) ;
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"UserAgent","","" ) ;
navigator.Value = Request.UserAgent ;
navigator.MoveToParent() ;
navigator.Insert(TreePosition.After, XmlNodeType.Element,"UserHostAddress","","" ) ;
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"UserHostAddress","","" ) ;
navigator.Value = Request.UserHostAddress ;
navigator.MoveToParent() ;
navigator.Insert(TreePosition.After, XmlNodeType.Element,"UserHostName","","" ) ;
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"UserHostName","","" ) ;
navigator.Value = Request.UserHostName ;
//to get more information of the users browsers capabilities make an object
//of the HttpBrowserCapabilities class
HttpBrowserCapabilities bc = Request.Browser;

navigator.MoveToParent() ;
navigator.Insert(TreePosition.After, XmlNodeType.Element,"BrowserType","","" ) ;
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"BrowserType","","" ) ;
navigator.Value = bc.Type ;

navigator.MoveToParent() ;
navigator.Insert(TreePosition.After, XmlNodeType.Element,"BrowserName","","" ) ;
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"BrowserName","","" ) ;
navigator.Value = bc.Browser ;

navigator.MoveToParent() ;
navigator.Insert(TreePosition.After, XmlNodeType.Element,"MajorVersion","","" ) ;
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"MajorVersion","","" ) ;
navigator.Value = bc.MajorVersion.ToString() ;

navigator.MoveToParent() ;
navigator.Insert(TreePosition.After, XmlNodeType.Element,"MinorVersion","","" ) ;
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"MinorVersion","","" ) ;
navigator.Value = bc.MinorVersion.ToString(); ;

navigator.MoveToParent() ;
navigator.Insert(TreePosition.After, XmlNodeType.Element,"Platform","","" ) ;
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Platform","","" ) ;
navigator.Value = bc.Platform ;

//Make an object of the DateTime class to get the Date Time
DateTime now = DateTime.Now ;
navigator.MoveToParent() ;
navigator.Insert(TreePosition.After, XmlNodeType.Element,"Date","","" ) ;
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Date","","" ) ;
navigator.Value = now.ToShortDateString() ;

navigator.MoveToParent() ;
navigator.Insert(TreePosition.After, XmlNodeType.Element,"Time","","" ) ;
navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Time","","" ) ;
navigator.Value = now.ToShortTimeString() ;
//Create a File Stream again to Write to the Database
//Again remember to specify the "FileShare.ReadWrite"
FileStream fout ;
fout = new FileStream(Server.MapPath(datafile), FileMode.Open, FileAccess.Write,
FileShare.ReadWrite) ;

//finally save the user data to the xml database file
xmldocument.Save(new StreamWriter(fout)) ;
//free the resources explicitly for other classes to use
fout.Close();
navigator=null ;
xmldocument=null ;
//Just store any value to the session
Session["counter"]=1 ;
}

}
catch(Exception edd)
{
//catch other exceptions
Response.Write("<font color=#FF0000>An Exception Occurred "+edd.ToString()+"</font>") ;
}

}
}

</script>
</head>

<body >

<h3 align="center">Welcome to Saurabh's Counter Script</h3>
<br>
<p align="center">
This is a sample page which has the counter scripting in it.
Take the script from this page and paste it on your page.

</p>
<asp:label text="You are visitor No: 0" style="font-size:28pt" id="countmess" runat="server" />


</body>

</html>





2) viewcounter.aspx : The counter information viewing page

<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Assembly Name="System.Xml" %>
<%@ Import Namespace="System.Xml" %>
<%@ Page Language="C#" %>
<html>
<head>
<title>Saurabh's XML Counter Script</title>
<script language="C#" runat=server>
//this script is execute when the page is loaded
public void Page_Load(Object sender, EventArgs e)
{
//the path to the Xml file which will contain all the data
//modify this if you have any other file or directory mappings.
//modify this if you have been directed here from Step 2 of the ReadMe file.
string datafile="db/xmlcounter.xml" ;
try
{
//Make an instance of the XmlDataDocument class which reads data from a
//xml file and stores it in an DataSet object
XmlDataDocument datadoc = new XmlDataDocument();

//Open a FileStream to the Database
//"FileShare.ReadWrite" enables other user to also read and write to the file
FileStream fin ;
fin = new FileStream(Server.MapPath(datafile), FileMode.Open, FileAccess.Read, FileShare.ReadWrite) ;
// Infer the DataSet schema from the XML data and load the XML Data
datadoc.DataSet.ReadXml(new StreamReader(fin));
//Close the stream
fin.Close();

//get the total no of viewers by getting the count of the no of rows present
//in the Table
showtotal.Text ="Total Viewers :"+ datadoc.DataSet.Tables[1].Rows.Count.ToString() ;

//databind the Repeater to the Dataset of table '1' ie the 'Viewer'
MyDataList.DataSource = datadoc.DataSet.Tables[1].DefaultView;
MyDataList.DataBind();

//free the resources
datadoc=null ;

}
catch (Exception ed)
{
//if there is any exception then display it
Response.Write("<font color=#FF0000>An Exception occured "+ed.ToString()+"</font>") ;
}
}
</script>
</head>
<body >
<h4>Welcome to Saurabh's XML Counter Viewing Page.</h4>
<asp:label id="showtotal" text="" runat="server" />
<br>

<ASP:Repeater id="MyDataList" runat="server">
<template name="headertemplate">
<h5> Viewer Details </h5>
</template>
<template name="itemtemplate">
<br>
<table class="mainheads" width="60%" style="font: 8pt verdana" >
<tr style="background-color:#FFFFCC">
<td>Referrer :</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Referrer") %>
</td></tr>
<tr style="background-color:#FFFFCC">
<td>User Agent :</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "UserAgent") %>
</td></tr>
<tr style="background-color:#FFFFCC">
<td>User Host Address :</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "UserHostAddress") %>
</td></tr>
<tr style="background-color:#FFFFCC">
<td>User Host Name :</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "UserHostName") %>
</td></tr>
<tr style="background-color:#FFFFCC">
<td>Browser Type :</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "BrowserType") %>
</td></tr>
<tr style="background-color:#FFFFCC">
<td>Browser Name :</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "BrowserName") %>
</td></tr>
<tr style="background-color:#FFFFCC">
<td>Major Version :</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "MajorVersion") %>
</td></tr>
<tr style="background-color:#FFFFCC">
<td>Minor Version :</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "MinorVersion") %>
</td></tr>
<tr style="background-color:#FFFFCC">
<td>Platform :</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Platform") %>
</td></tr>
<tr style="background-color:#FFFFCC">
<td>Date :</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Date") %>
</td></tr>


<tr style="background-color:#FFFFCC">
<td>Time :</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Time") %>
</td>
</tr>
</table><br>
</template>
</ASP:Repeater>
</body>
</html>

责任编辑:

收藏本文 打印 打印本文  推荐本文 告诉好友 投稿 投稿邮箱

站长排行

学院

新闻

专栏

盈利

[揭密网络黄链]中国留学生买凶专破日本
JSP语法(6)
超强弹出窗口代码,什么都挡不住
FLASH视觉特效实例之地震效果
贴吧发帖机使用教程(绝对原创)
关于数据分页(转自www.codeproject.co
ASP实现文件直接下载
Photoshop制作光感超酷效果水晶球
 遍历ASP.NET页面控件
永远的后门[经典]+查不出的后门
淘宝网卖家公然叫卖“艳照门”照片集
驳《百度Hi面世对腾讯有利》
Google绿色专家质疑黑色背景网页节省资
国内各IT企业办公环境揭秘(多图)
阿里妈妈广告卖主全攻略
站长创业源动力 主流站长站赏析
推荐阅读:80年小子的创业道理
Discuz!6.0猛将出击 最强论坛程序酷炫
我的网络,我的团队:专访李文明
百度新闻频道改版十天 流量止跌反弹翻
ECSHOP模板制作参考文档
悬挂阿里妈妈会否被百度惩罚
阿里妈妈是否是中小站长的救世主?
最强网店ECShop发新版 众多酷炫功能给
ECSHOP模板下载
土豆网,优酷网,爆米花等视频网站采集
DedeCms模板安装/制作概述
网上商店系统巅峰对决 ECShop vs ShopE
艰难的走在创业的路上 第一天
编程中国全站采集规则
性福联盟 一个不尊重站长的联盟
大脚:日赚100元—揭露最新firefox欺骗
大脚:垃圾站超级赚钱法之二—突破“站
大脚:垃圾站超级赚钱法之——前言
迅雷联盟、快车联盟收入对比
经理人必看的十个管理网站
Google Adsense的秘密 第二版
西联汇款兑付城市查询
不用SEO取得成功的10个步骤
关于做GOOGLE的五条经验
站长学院  网页设计 建站教程 图形图象 网络编程

Photoshop CS3
Photoshop CS3
不用Photoshop
不用Photoshop

DIV+CSS的开发方式 听听另外的
虚拟主机建站动易里快速生成的
VBScript特效代码 满屏幕乱跑
牛气!一个菜鸟站长的超强网站
创建、维护一个个人博客的“投
让网站流量稳步飙升的秘籍
网站推广的基本思想

新闻线索

如果你有站长界人事变动、重组并购、变革技术出现,以及产品投诉等重要新闻线索,请告诉我们,我们会给予特别关注。
0631-3653338
站长中国编辑部
站长中国24小时新闻热线: 13256307008