ASP.NET XML编写计数器

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 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 , 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","","") ;

时间: 2024-08-31 17:52:02

ASP.NET XML编写计数器的相关文章

论Asp与XML的关系

xml  1.XML 是什么?    XML仅仅是一种数据存放格式,这种格式是一种文本(虽然XML规范中也提供了存放二进制数据的解决方案).     事实上有很多文本格式都可以用来存放数据,例如大家所熟悉的.ini文件.很多朋友在初学C语言或者Basic语言的时候,有时可能需要将源数据或者最终结果存放在一个文本文件里面,存放的格式当然由编写程序的人自己定了,那么在编写这个程序的过程中,编程者就自创了一种自定义的数据格式.     XML格式本身也是一种存放数据的格式,和您当时自己定义的这种数据文

浅谈Asp与XML的关系

xml     1.XML 是什么? XML仅仅是一种数据存放格式,这种格式是一种文本(虽然XML规范中也提供了存放二进制数据的解决方案). 事实上有很多文本格式都可以用来存放数据,例如大家所熟悉的.ini文件.很多朋友在初学C语言或者Basic语言的时候,有时可能需要将源数据或者最终结果存放在一个文本文件里面,存放的格式当然由编写程序的人自己定了,那么在编写这个程序的过程中,编程者就自创了一种自定义的数据格式. XML格式本身也是一种存放数据的格式,和您当时自己定义的这种数据文件本质上并无什么

ASP.Net+XML打造留言薄

asp.net|xml 一.概述: 留言簿是网站的一个重要组成部分,是访问者发表意见的场所,也是网站管理员了解网站基本运行情况的有力工具,所以留言簿在现在的网站中扮演了十分重要的角色. 不过在以前开发一个留言簿并不是一件容易的事,开发者的工作量往往会很大.而现在随着微软推出VS.NET,相应的技术也推陈出新.特别是XML技术在.NET Framework中的广泛运用,使得整个.NET构架具有十分优越的基础.而ASP.NET中推出的崭新的编程模型更使得开发Web应用程序变得非常容易.本文就结合AS

Asp.net+Xml实现无数据库论坛一点即通

asp.net|xml|数据|数据库 起草语:流行论坛(我们重点讨论ASP,ASP.NET论坛)的开发思路 老手可略过此节,就读下节. 目前,网上论坛各式各样各种版本不计其数,归根结底如何神通其核心数据存取仓库都是"数据库",我是指我们常用的ACCESS SQLSERVER等等这种形式的"数据库",而后由PHP ASP ASP.NET JSP等等这些后台语言通过中心"ADO,SQL"两条线,像伐木搭桥似的一小块一小块(会员注册.会员登陆.论坛栏目

一个Asp与XML交互的实例源码

XML 是标准扩展语言,是未来Web编程的标准,asp 是现在广为流传的web编程语言之一,能不能让他们两个联合起来发挥作用呢?豆腐在这里给大家提供一个很简单的例子关于XML和XSL限于篇幅和知识水平豆腐就不在这里献丑了下面首先来说说几个需要用到的文件的内容. testXsl.xsl: <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:te

asp写入xml乱码问题

如果你的操作系统是中文系统, 1.你新建xml文档,默认是gb2312编码的. <?xml version="1.0" encoding="gb2312" ?> 绝对可以显示中文. 2,你是改的别人的xml文档,但之前该xml文档是UTF-8/16编码的, <?xml version="1.0" encoding="gb2312" ?>不能显示中文,因为该文档不是gb2312编码的,虽然你强制申明它要用

asp查询xml的代码,实现了无刷新、模糊查询功能

xml|刷新|无刷新 asp查询xml的代码,实现了无刷新.模糊查询功能 <html><head><title>不刷新页面查询的方法</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><script language="javascript"><

ASP读取XML数据文件的方法

xml|数据 分别保存下面两段代码,一个保存为readxml.asp另一个保存为test.xml,放在同一个目录下面,调试程序即可,在程序里面我已经做了解释,读取代码可以做成一个readxml的函数,通过使用输入的参数而读取xml不同数据记录的不同的值.这段程序的改编来自互联网,有什么出入请见谅. readxml.asp以下是引用片段:<% dim xml,objNode,objAtr,nCntChd,nCntAtr Set xml=Server.CreateObject("Microso

浅谈ASP程序的编写和调试:给初学者

程序|初学 很多朋友觉得ASP不是很好学,我觉得其实有了一定的编程基础,再加上那么一点点技巧,入门是不难的(不过要做好也不是很容易,要很多的相关知识--什么Sql Server数据库语言啦,数据结构啦(这些都是在优化Asp的算法时候要熟悉的). 在这只和大家谈谈我个人是怎书写和调试ASP的,希望能给大有一点帮助-- ASP脚本的书写: 一.注意区分清楚html语法和VBScript语法.因为ASP是混合使用Html语法和VBScript语法的(当然,我全部写成VBScript语法也可以,不过那样