将观看统计数据写入 XML 文件。丢失数据计数可能会出现问题吗?
您好,我已经创建了 XML,其中包含属性代码和该代码的页面浏览次数:
<?xml version="1.0" encoding="utf-8"?>
<data>
<nehnutelnost code="BSPO066P">35</nehnutelnost>
<nehnutelnost code="PMDM029P">4</nehnutelnost>
</data>
我想我发现了可能的弱点,今天所有计数器都消失了,一切都从头开始。
这是检查 XML 文件中代码是否存在的代码,然后添加 +1 或使用 1 创建新的 XML 标记。
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"));
我的问题是:
是否可能有其他人同时打开页面和文件刚刚被覆盖或以某种方式重置了数据?
是否有其他方法可以保留页面访问者的数量,以便将来不会重置?
是否
谢谢。
费罗
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:
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?
is there any other way how to keep number if page visitors so it wont reset in future?
Thank you.
Fero
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎遇到了并发问题。
您可以锁定写入操作,但是您必须注意您正在为您的应用程序创建一个可能的瓶颈。
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.