<%@ 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
<title>saurabh ' s XML Counter script</title>
<script language= "C #" runat= "Server" >
The script is called when the page is loaded
public void Page_Load (Object src, EventArgs e)
{
The path to the Xml file which would contain all the data
Modify this if you are have any other file or directory mappings.
Modify this if your have been directed here's 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 is missing in me last release hence there is 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 a instance of the Documentnavigator class used to
Navigate through and XML file
Documentnavigator navigator = new Documentnavigator (XmlDocument);
Move to the ' visitors '
Navigator. Movetodocumentelement ();
Move to it child at position ' 0 ' (ie.in Me file ' total ' node)
Navigator. Movetochild (0);
Check if we are on the right element which has
if (navigator. HasAttributes) {
Get the attributes of the node ' total ' called ' tot ' (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 sessions 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'll contain all
The information of a single visitor
Navigator. Insert (Treeposition.after, XmlNodeType.Element, "Viewer", "", "");
Make a instance to the "Httpurl class to" 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's 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", "", "");
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.