XML Question

Last post 05-14-2008 6:59 PM by haoest. 1 replies.

Sort Posts:

  • XML Question

    05-14-2008, 4:45 PM
    • Loading...
    • hesed4him
    • Joined on 05-05-2007, 3:28 AM
    • Deerfield Beach, FL
    • Posts 210

    I have a script that uses XML files as log files.  For example...

    Everytime I access A.aspx the script is ran and appends an entry containing the date/time and referrer to the xml file.

    A major stripped down verion of the method is:

        XmlDocument xmlDoc = new XmlDocument();

        XmlElement entry = xmlDoc.CreateElement("entry");
        xmlDoc.Load(logFile);
        xmlDoc.DocumentElement.AppendChild(entry);

        xmlDoc.Save(logFile);

     

    My question is: If the server page is accessed almost simultaneously by two different users, how can I make sure that BOTH "entry" elements are added to the xml file?  My fear is that User 1 loads the file immediately before User 2.  User 1 appends the element and saves the file, then User 2 appends an element and saves the file again, but overwriting User 1's save, thus eliminating User 1's update to the log file.  Again, how can I prevent this?

     Thanks,
    Joshua

    Dont forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: XML Question

    05-14-2008, 6:59 PM
    Answer
    • Loading...
    • haoest
    • Joined on 10-25-2005, 4:20 PM
    • Posts 399

    might not be the most bullet proof solutions:

       XmlDocument xmlDoc = new XmlDocument();

    Lock(xmlDoc){ 

        XmlElement entry = xmlDoc.CreateElement("entry");
        xmlDoc.Load(logFile);
        xmlDoc.DocumentElement.AppendChild(entry);

        xmlDoc.Save(logFile);

     }

    Debugger is my best friend. (http://haoest.info)
Page 1 of 1 (2 items)