views:

24

answers:

1

Hello I have created XML which holds codes for properties and number of page viewings for that code:

<?xml version="1.0" encoding="utf-8"?>
<data>
    <nehnutelnost code="BSPO066P">35</nehnutelnost>
     <nehnutelnost code="PMDM029P">4</nehnutelnost>
</data>

I think I have found possible weakness with this, today all counters disapeared and all started from scratch.

This is the code which checks for code existence in XML file and either add +1 or create new XML tag with 1.

        int intSeenCount = 0;
        XDocument xmlSeenCount = new XDocument();
        xmlSeenCount = XDocument.Load(Server.MapPath(@"App_Data\lozjoCounts.xml"));

        XElement xmlElement = xmlSeenCount.XPathSelectElement("data/nehnutelnost[@code = '" + strCisloZakazky + "']");

            if (xmlElement == null)
            {
                xmlElement = (new XElement("nehnutelnost",
                                                        new XAttribute("code", strCisloZakazky),
                                                        ++intSeenCount));
                xmlSeenCount.Element("data").Add(xmlElement);
            }
            else
            {
                intSeenCount = (Convert.ToInt32(xmlElement.Value) + 1);
                xmlElement.Value = intSeenCount.ToString();
            }
            xmlSeenCount.Save(Server.MapPath(@"App_Data\lozjoCounts.xml"));

My questions would be:

  1. is it possible that there was page opened by someone else at the same time and file just got overwritten or somehow reseted the data?

  2. is there any other way how to keep number if page visitors so it wont reset in future?

Thank you.

Fero

A: 

It seems you are having a concurrency problem.

You could lock the writing operation, but then you must be aware that your are creating a possible bottleneck in you application.

santiiiii