您现在的位置: 军旅同心-旅游自驾-军旅文学 >> 读书赏析 >> 学习园地 >> 电脑网络 >> 技术文章 >> 正文
ASP.NET: XML计数器第二版
作者:采集员 文章来源:来源于网络 点击数: 更新时间:2005-9-10 12:36:09
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
FileStre

[1] [2] 下一页


更多
免责声明:作品版权归所属媒体与作者所有!!本站刊载此文不代表同意其说法或描述,仅为提供更多信息。如果您认为我们侵犯了您的版权,请告知!本站立即删除。有异议请联系我们。
文章录入:烟灰缸    责任编辑:烟灰缸 
网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
| 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 网站地图 | 版权申明 | 网站公告 | 管理登录 |