设为首页 加入收藏 网站搜索 繁體中文 中国建站网 — 站长资源分享平台

ASP.NET: XML计数器第二版

来源:网络收集 作者:佚名 时间:2007-04-25 18:27:16
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

[1] [2] [3]  下一页

Tags:

  • 好的评价 如果您觉得好,就请您
      0%(0)
  • 差的评价 如果您觉得差,就请您
      0%(0)
  • 相关文章
    广告赞助

    文章随便看看 设计素材 建站学院 网页模板 视频教程

    网友评论

    共有 0 位网友发表了评论,得分 0 分,平均 0 分    查看完整评论

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 1分

    内 容:

             通知管理员 验证码: