ASP.NET: XML计数器第二版

Code:

  1. counter.aspx :- The Counter Page
1@ Import Namespace="System.IO" 
1@ Assembly Name="System.Xml" 
1@ Import Namespace="System.Xml" 
1@ page language="c#" EnableSessionState="True" 
1-- These are the imported assembiles and namespaces need to run the counter --
  1<html>
  2<head>
  3<title>Saurabh's XML Counter Script</title>
  4<script language="C#" runat="server">   
  5//script is called when the page is loaded   
  6public void Page_Load(Object src, EventArgs e)   
  7{   
  8//the path to the Xml file which will contain all the data   
  9//modify this if you have any other file or directory mappings.   
 10//modify this if you have been directed here from Step 2 of the ReadMe file.   
 11string datafile="db/xmlcounter.xml" ;   
 12  
 13if(!Page.IsPostBack){   
 14//try-catch block containing the counter code   
 15try {   
 16//create an instance of the class XmlDocument   
 17XmlDocument xmldocument = new XmlDocument() ;   
 18  
 19//Open a FileStream to the specified file   
 20FileStream fin ;   
 21//It is very Important to specify the "FileShare.ReadWrite" option.   
 22//This allows other viewers to also read and write to the Database   
 23//This was missing in my last release hence there was a BUG !!!   
 24fin = new FileStream(Server.MapPath(datafile), FileMode.Open, FileAccess.Read,   
 25FileShare.ReadWrite) ;   
 26//Load the Document   
 27xmldocument.Load(new StreamReader(fin)) ;   
 28fin.Close();   
 29//create an instance of the DocumentNavigator class used to   
 30//navigate through and XML file   
 31DocumentNavigator navigator = new DocumentNavigator(xmldocument) ;   
 32  
 33//Move to the first element (in my file 'Visitors')   
 34navigator.MoveToDocumentElement() ;   
 35//move to it child at position '0' (ie.in my file 'total' node)   
 36navigator.MoveToChild(0) ;   
 37  
 38//check if we are on the right element which has an attribute   
 39if (navigator.HasAttributes) {   
 40//get the attribute of the node 'total' called 'tot' (see the xmlcounter.xml file)   
 41//since the value stored is in a string format we 'cast' it into a Int type   
 42int total = int.Parse(navigator.GetAttribute("tot")) ;   
 43//increase the counter   
 44total++ ;   
 45//show the counter on the page   
 46countmess.Text = "You are visitor No: "+total.ToString() ;   
 47//save the incremented counter back in the XML file   
 48navigator.SetAttribute(0,total.ToString() );   
 49}   
 50  
 51//Update the Database only if a new session is there   
 52if(Session["counter"]==null)   
 53{   
 54//move back to the Document element   
 55navigator.MoveToDocumentElement() ;   
 56navigator.MoveToChild(0) ;   
 57//then insert the element after the 'total' element which will contain all   
 58//the information of a single visitor   
 59navigator.Insert(TreePosition.After , XmlNodeType.Element, "Viewer","","") ;   
 60//make an instance to the HttpUrl class to get information of the referrer to   
 61//the page if any. if there are no referrers then by Default this object is 'null'   
 62//so we have to make a check if it is null and do the needful   
 63HttpUrl objUrl = Request.UrlReferrer;   
 64if(objUrl!=null)   
 65{   
 66navigator.Insert(TreePosition.FirstChild, XmlNodeType.Element,"Referrer","","");   
 67navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Referrer","","") ;   
 68navigator.Value = objUrl.Host ;   
 69}   
 70else   
 71{   
 72navigator.Insert(TreePosition.FirstChild, XmlNodeType.Element,"Referrer","","");   
 73navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Referrer","","") ;   
 74navigator.Value = "Direct" ;   
 75}   
 76//release the resource for Garbage collection   
 77objUrl=null ;   
 78//move to parent node and then insert the information about the useragent   
 79navigator.MoveToParent() ;   
 80navigator.Insert(TreePosition.After, XmlNodeType.Element,"UserAgent","","" ) ;   
 81navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"UserAgent","","" ) ;   
 82navigator.Value = Request.UserAgent ;   
 83navigator.MoveToParent() ;   
 84navigator.Insert(TreePosition.After, XmlNodeType.Element,"UserHostAddress","","" ) ;   
 85navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"UserHostAddress","","" ) ;   
 86navigator.Value = Request.UserHostAddress ;   
 87navigator.MoveToParent() ;   
 88navigator.Insert(TreePosition.After, XmlNodeType.Element,"UserHostName","","" ) ;   
 89navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"UserHostName","","" ) ;   
 90navigator.Value = Request.UserHostName ;   
 91//to get more information of the users browsers capabilities make an object   
 92//of the HttpBrowserCapabilities class   
 93HttpBrowserCapabilities bc = Request.Browser;   
 94  
 95navigator.MoveToParent() ;   
 96navigator.Insert(TreePosition.After, XmlNodeType.Element,"BrowserType","","" ) ;   
 97navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"BrowserType","","" ) ;   
 98navigator.Value = bc.Type ;   
 99  
100navigator.MoveToParent() ;   
101navigator.Insert(TreePosition.After, XmlNodeType.Element,"BrowserName","","" ) ;   
102navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"BrowserName","","" ) ;   
103navigator.Value = bc.Browser ;   
104  
105navigator.MoveToParent() ;   
106navigator.Insert(TreePosition.After, XmlNodeType.Element,"MajorVersion","","" ) ;   
107navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"MajorVersion","","" ) ;   
108navigator.Value = bc.MajorVersion.ToString() ;   
109  
110navigator.MoveToParent() ;   
111navigator.Insert(TreePosition.After, XmlNodeType.Element,"MinorVersion","","" ) ;   
112navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"MinorVersion","","" ) ;   
113navigator.Value = bc.MinorVersion.ToString(); ;   
114  
115navigator.MoveToParent() ;   
116navigator.Insert(TreePosition.After, XmlNodeType.Element,"Platform","","" ) ;   
117navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Platform","","" ) ;   
118navigator.Value = bc.Platform ;   
119  
120//Make an object of the DateTime class to get the Date Time   
121DateTime now = DateTime.Now ;   
122navigator.MoveToParent() ;   
123navigator.Insert(TreePosition.After, XmlNodeType.Element,"Date","","" ) ;   
124navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Date","","" ) ;   
125navigator.Value = now.ToShortDateString() ;   
126  
127navigator.MoveToParent() ;   
128navigator.Insert(TreePosition.After, XmlNodeType.Element,"Time","","" ) ;   
129navigator.Insert(TreePosition.FirstChild, XmlNodeType.Text,"Time","","" ) ;   
130navigator.Value = now.ToShortTimeString() ;   
131//Create a File Stream again to Write to the Database   
132//Again remember to specify the "FileShare.ReadWrite"   
133FileStream fout ;   
134fout = new FileStream(Server.MapPath(datafile), FileMode.Open, FileAccess.Write,   
135FileShare.ReadWrite) ;   
136  
137//finally save the user data to the xml database file   
138xmldocument.Save(new StreamWriter(fout)) ;   
139//free the resources explicitly for other classes to use   
140fout.Close();   
141navigator=null ;   
142xmldocument=null ;   
143//Just store any value to the session   
144Session["counter"]=1 ;   
145}   
146  
147}   
148catch(Exception edd)   
149{   
150//catch other exceptions   
151Response.Write("<font color=#FF0000>An Exception Occurred "+edd.ToString()+"</font>") ;   
152}   
153  
154}   
155}   
156  
157</script>
158</head>
159<body>
160<h3 align="center">Welcome to Saurabh's Counter Script</h3>
161<br/>
162<p align="center">   
163This is a sample page which has the counter scripting in it.   
164Take the script from this page and paste it on your page.   
165  
166</p>
167<asp:label id="countmess" runat="server" style="font-size:28pt" text="You are visitor No: 0"></asp:label>
168</body>
169</html>
  1. viewcounter.aspx : The counter information viewing page
1@ Import Namespace="System" 
1@ Import Namespace="System.IO" 
1@ Import Namespace="System.Data" 
1@ Assembly Name="System.Xml" 
1@ Import Namespace="System.Xml" 
1@ Page Language="C#" 
 1<html>
 2<head>
 3<title>Saurabh's XML Counter Script</title>
 4<script language="C#" runat="server">   
 5//this script is execute when the page is loaded   
 6public void Page_Load(Object sender, EventArgs e)   
 7{   
 8//the path to the Xml file which will contain all the data   
 9//modify this if you have any other file or directory mappings.   
10//modify this if you have been directed here from Step 2 of the ReadMe file.   
11string datafile="db/xmlcounter.xml" ;   
12try   
13{   
14//Make an instance of the XmlDataDocument class which reads data from a   
15//xml file and stores it in an DataSet object   
16XmlDataDocument datadoc = new XmlDataDocument();   
17  
18//Open a FileStream to the Database   
19//"FileShare.ReadWrite" enables other user to also read and write to the file   
20FileStream fin ;   
21fin = new FileStream(Server.MapPath(datafile), FileMode.Open, FileAccess.Read, FileShare.ReadWrite) ;   
22// Infer the DataSet schema from the XML data and load the XML Data   
23datadoc.DataSet.ReadXml(new StreamReader(fin));   
24//Close the stream   
25fin.Close();   
26  
27//get the total no of viewers by getting the count of the no of rows present   
28//in the Table   
29showtotal.Text ="Total Viewers :"+ datadoc.DataSet.Tables[1].Rows.Count.ToString() ;   
30  
31//databind the Repeater to the Dataset of table '1' ie the 'Viewer'   
32MyDataList.DataSource = datadoc.DataSet.Tables[1].DefaultView;   
33MyDataList.DataBind();   
34  
35//free the resources   
36datadoc=null ;   
37  
38}   
39catch (Exception ed)   
40{   
41//if there is any exception then display it   
42Response.Write("<font color=#FF0000>An Exception occured "+ed.ToString()+"</font>") ;   
43}   
44}   
45</script>
46</head>
47<body>
48<h4>Welcome to Saurabh's XML Counter Viewing Page.</h4>
49<asp:label id="showtotal" runat="server" text=""></asp:label>
50<br/>
51<asp:repeater id="MyDataList" runat="server">
52<template name="headertemplate">
53<h5> Viewer Details </h5>
54</template>
55<template name="itemtemplate">
56<br/>
57<table class="mainheads" style="font: 8pt verdana" width="60%">
58<tr style="background-color:#FFFFCC">
59<td>Referrer :</td>
60<td>   

DataBinder.Eval(Container.DataItem, "Referrer")

1</td></tr>
2<tr style="background-color:#FFFFCC">
3<td>User Agent :</td>
4<td>   

DataBinder.Eval(Container.DataItem, "UserAgent")

1</td></tr>
2<tr style="background-color:#FFFFCC">
3<td>User Host Address :</td>
4<td>   

DataBinder.Eval(Container.DataItem, "UserHostAddress")

1</td></tr>
2<tr style="background-color:#FFFFCC">
3<td>User Host Name :</td>
4<td>   

DataBinder.Eval(Container.DataItem, "UserHostName")

1</td></tr>
2<tr style="background-color:#FFFFCC">
3<td>Browser Type :</td>
4<td>   

DataBinder.Eval(Container.DataItem, "BrowserType")

1</td></tr>
2<tr style="background-color:#FFFFCC">
3<td>Browser Name :</td>
4<td>   

DataBinder.Eval(Container.DataItem, "BrowserName")

1</td></tr>
2<tr style="background-color:#FFFFCC">
3<td>Major Version :</td>
4<td>   

DataBinder.Eval(Container.DataItem, "MajorVersion")

1</td></tr>
2<tr style="background-color:#FFFFCC">
3<td>Minor Version :</td>
4<td>   

DataBinder.Eval(Container.DataItem, "MinorVersion")

1</td></tr>
2<tr style="background-color:#FFFFCC">
3<td>Platform :</td>
4<td>   

DataBinder.Eval(Container.DataItem, "Platform")

1</td></tr>
2<tr style="background-color:#FFFFCC">
3<td>Date :</td>
4<td>   

DataBinder.Eval(Container.DataItem, "Date")

1</td></tr>
2<tr style="background-color:#FFFFCC">
3<td>Time :</td>
4<td>   

DataBinder.Eval(Container.DataItem, "Time")

1</td>
2</tr>
3</table><br/>
4</template>
5</asp:repeater>
6</body>
7</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus