ASP. NET writes counters in combination with XML

Source: Internet
Author: User
Reference content is as follows:

Code:
1) counter. aspx:-The Counter Page

<% @ Import Namespace = "System. IO" %>
<% @ Assembly Name = "System. xml (standardization is getting closer and closer)" %>
<% @ Import Namespace = "System. xml (standardization is getting closer and closer)" %>
<% @ Page language = "c #" EnableSessionState = "True" %>
<% -- These are the imported assembiles and namespaces need to run the counter -- %>
<Html>
<Head>
<Title> Saurabh's xml (standardization is getting closer and closer) 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 (standardization is getting closer and closer) 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 (standardization is getting closer and closer) counter. xml (standardization is getting closer and closer )";

If (! Page. IsPostBack ){
// Try-catch block containing the counter code
Try {
// Create an instance of the class xml (standardization is getting closer and closer) Document
Xml (standardization is getting closer and closer) Document xml (standardization is getting closer and closer) document = new xml (standardization is getting closer and closer) 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 (standardization is getting closer and closer) document. Load (new StreamReader (fin ));
Fin. Close ();
// Create an instance of the DocumentNavigator class used
// Navigate through and xml (standardization is getting closer and closer) file
DocumentNavigator navigator = new DocumentNavigator (xml (standardization is getting closer and closer) 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 (standardization is getting closer and closer) counter. xml (standardization is getting closer and closer) 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 (standardization is getting closer and closer) 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 (standardization is getting closer and closer) NodeType. Element, "Viewer ","","");
// Make an instance to the HttpUrl class to get information of the referrer
// 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 (standardization is getting closer and closer) NodeType. Element, "Referrer ","","");
Navigator. Insert (TreePosition. FirstChild, xml (standardization is getting closer and closer) NodeType. Text, "Referrer ","","");

 

Related Article

Contact Us

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.

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.