Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 12th, 2007, 06:40 AM
Authorized User
 
Join Date: Nov 2007
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default Read and update/increment XML doc from aspx

Hi



I have a counter.xml file, this holds a counter value, on my website users enter in information , when the click on a button I want my page to retrieve the <CurrentCount> value from my XML doc , increment it by one so it can be used locally as a session variable and I went it to increment by 1on the XML file also.



 any ideas?

XML:

<Counter>

  <CurrentCount>

    5990000000

  </CurrentCount>

</Counter>
__________________
Thank You
 
Old December 12th, 2007, 06:59 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

What part of this are you having trouble with? The basic steps, assuming ASP.NET, are:
  • Open the XML file in an XmlDocument
  • Retrieve the CurrentCount element using the XPath /*/CurrentCount
  • Read the element's text and convert to an Int64
  • Increment the counter and set the elements value
  • Save the file
However you are going to have to deal with concurrency issues when more than one user tries to write to the file. You can probably do this by calling Application.Lock() and UnLock() but this will block requests and slow down page loading etc. I'm not sure using an XML file for read/write data is a good idea for this type of use.

--

Joe (Microsoft MVP - XML)
 
Old December 12th, 2007, 07:14 AM
Authorized User
 
Join Date: Nov 2007
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default

hi

thanks for this, i will try implemnet it now.
Regarding the secirity unfortunately we are not allowed use any others means than an XML doc for this, i do agree with you concerns though.
can i ask, at which point should i enforce the application.lock and unlock methods?
 
Old December 12th, 2007, 08:26 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Either side of opening, reading and re-saving the file. Try to minimise thye number of commands between locking and unlocking.

--

Joe (Microsoft MVP - XML)
 
Old December 12th, 2007, 10:08 AM
Authorized User
 
Join Date: Nov 2007
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default

Hi

Here’s my code, now I have a variable which contains the counter value incremented by 1 (lngCount), how do I now save that value back to my XML element.
Also is there a way of check that the value im saving back is alreay there, and If it is I need to increase it again?

     protected void btn_count_Click(object sender, EventArgs e)
    {
        DataSet dsCount = new DataSet();

        dsCount.ReadXml(Server.MapPath("App_Data\\Counter. xml"));
        XmlDataDocument xmlDataCount = new XmlDataDocument(dsCount);

        foreach (XmlNode xmlCountNodes in xmlDataCount.SelectSingleNode("//CurrentCount"))
        {
            txt_Counter.Text = xmlCountNodes.InnerText.Trim().ToString();

        }



        long lngCount = new long();
        lngCount = Convert.ToInt64(txt_Counter.Text);
        lngCount = lngCount + 1;
        txt_New_count.Text = lngCount.ToString();
    }
 
Old December 12th, 2007, 10:15 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You are loading the XML data into a DataSet, then converting the DataSet into XML - thats a bit wasteful.

Just use XmlDocument and the Load() and Save() methods.

/- Sam Judson : Wrox Technical Editor -/
 
Old December 12th, 2007, 01:23 PM
Authorized User
 
Join Date: Nov 2007
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Hughesie78
Default

thanks, heres what i settled on, what do you think:
; protected void btn_count_Click(object sender, EventArgs e)
    {
       XmlDocument xmlDocCounter = new XmlDocument();
        xmlDocCounter.Load(Server.MapPath("App_Data\\Count er.xml"));

        XmlNode xmlCountnode;
        XmlNode root = xmlDocCounter.DocumentElement;

        xmlCountnode = root.SelectSingleNode("//CurrentCount");
        long lngCount = new long();
        lngCount = Convert.ToInt64(xmlCountnode.InnerText.Trim());
        lngCount = lngCount + 1;

        xmlCountnode.InnerText = lngCount.ToString();
        xmlDocCounter.Save(Server.MapPath("App_Data\\Count er.xml"));
    }
 
Old December 13th, 2007, 06:18 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I would change it to a function, I don't like code in event handlers, other than UI manipulation, as it's hard to maintain and re-use:
Code:
private long GetCurrentCount(string path)
{
  XmlDocument xmlDocCounter = new XmlDocument();
  Application.Lock();  
  xmlDocCounter.Load(path);
  XmlElement xmlCountnode = xmlDocCounter.SelectSingleNode("/*/CurrentCount") as XmlElement; //That's a more efficient XPath
  long lngCount = Convert.ToInt64(xmlCountnode.InnerText.Trim());
  xmlCountnode.InnerText = (++lngCount).ToString();
  xmlDocCounter.Save(path);
  Appllication.Unlock();

  return lngCount;
}
I would then call it as:
Code:
long lngCount = GetCurrentCount(Server.MapPath("App_Data\\Counter.xml"));
form the click event handler.
(I'd have the path to the file in web.config too.)


--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating XML doc ; writing string(xml format) into KamalRaturi XML 5 May 28th, 2008 05:51 AM
Pull and Assign XSLT Parameter from ASPX Doc kwilliams ASP.NET 2.0 Basics 0 October 20th, 2006 11:35 AM
How to read *.doc file in asp.net satish1234 ASP.NET 1.0 and 1.1 Professional 2 August 8th, 2006 05:32 AM
DTS Package, XML task. Read XML file and store it Victoria SQL Server DTS 0 July 24th, 2006 02:43 PM
How to make stream reader read doc files? bcmaverik VB.NET 2002/2003 Basics 2 March 10th, 2004 10:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.