ASP.NET結合XML編寫計數器

來源:互聯網
上載者:User
以下為引用的內容:

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/xml(標準化越來越近了)counter.xml(標準化越來越近了)" ;

if(!Page.IsPostBack){
//try-catch block containing the counter code
try {
//create an instance of the class xml(標準化越來越近了)Document
xml(標準化越來越近了)Document xml(標準化越來越近了)document = new xml(標準化越來越近了)Document() ;

//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
xml(標準化越來越近了)document.Load(new StreamReader(fin)) ;
fin.Close();
//create an instance of the DocumentNavigator class used to
//navigate through and xml(標準化越來越近了) file
DocumentNavigator navigator = new DocumentNavigator(xml(標準化越來越近了)document) ;

//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 xml(標準化越來越近了)counter.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 N "+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 , xml(標準化越來越近了)NodeType.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, xml(標準化越來越近了)NodeType.Element,"Referrer","","");
navigator.Insert(TreePosition.FirstChild, xml(標準化越來越近了)NodeType.Text,"Referrer","","") ;

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.