if-else逻辑处理问题

gaeml 2010-09-07 05:23:26
实现功能:统计点击量,并更新xml文件中的点击次数。
现在问题:在xml文件添加不了新的数据


[WebMethod]
public string clickrating(string newstitle, string url, string dir, string auditortime, string newssort)
{
string path = Server.MapPath("../pages/clickrating/clickrating.xml");
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(path);

XmlElement xmlRoot = xmldoc.DocumentElement;
XmlElement attribute = xmldoc.CreateElement("item");

XmlNode xmlnode = xmldoc.DocumentElement;
int count = xmlRoot.ChildNodes.Count;
int tid = 0, id = 0, hit = 0;

if (count == 0)
{
attribute.SetAttribute("newsid", url.Remove(url.LastIndexOf(".html")));
attribute.SetAttribute("newstitle", newstitle);
attribute.SetAttribute("url", "" + dir + "/" + url.Remove(url.LastIndexOf("?")));
attribute.SetAttribute("auditortime", auditortime);
attribute.SetAttribute("newssort", newssort);
attribute.SetAttribute("hit", "0");
}
else
{
for (int i = 0; i <= count; i++)
{
tid = int.Parse(url.Remove(url.LastIndexOf(".html")));
id = int.Parse(xmlnode.ChildNodes[i].Attributes["newsid"].Value);
if (tid == id)
{
hit = int.Parse(xmlnode.ChildNodes[i].Attributes["hit"].Value);
xmlnode.ChildNodes[i].Attributes["hit"].Value = (hit + 1).ToString();
xmldoc.Save(path);
}
else
{
attribute.SetAttribute("newsid", url.Remove(url.LastIndexOf(".html")));
attribute.SetAttribute("newstitle", newstitle);
attribute.SetAttribute("url", "" + dir + "/" + url.Remove(url.LastIndexOf("?")));
attribute.SetAttribute("auditortime", auditortime);
attribute.SetAttribute("newssort", newssort);
attribute.SetAttribute("hit", "0");

xmlRoot.InsertBefore(attribute, xmlRoot.FirstChild);
xmldoc.Save(path);
}

//把上面的else语句放置在这就添加不成功
}
}
xmlRoot.InsertBefore(attribute, xmlRoot.FirstChild);
xmldoc.Save(path);
return newstitle;
}
...全文
169 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaeml 2010-09-08
  • 打赏
  • 举报
回复
是遍历循环的问题,不是修改的问题,循环体在循环列表时候,如果循环没有命中,但是被循环列表里有当前要求循环命中的值。
wuyq11 2010-09-07
  • 打赏
  • 举报
回复
href 中点击
通过ajax修改相关XML
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("a.xml");
XmlNodeList nodeList=xmlDoc.SelectSingleNode("").ChildNodes;
foreach(XmlNode xn in nodeList)
{
XmlElement xe=(XmlElement)xn;
if(xe.GetAttribute("")=="")
{
xe.SetAttribute("","");
}
}
xmlDoc.Save("a.xml");
或XmlElement e = (XmlElement)xmldoc.SelectSingleNode("//[@newsid='']"
e.SetAttribute( "", "");
Nick黄 2010-09-07
  • 打赏
  • 举报
回复
把else换下
if(true)

哈哈,开个玩笑
gaeml 2010-09-07
  • 打赏
  • 举报
回复
好像挺简单的逻辑,什么处理起来就是这么难呢,我哪里出了问题,各位指教啊
gaeml 2010-09-07
  • 打赏
  • 举报
回复
不是权限问题的,第一个if里的能添加成功,嵌套里的if也可以修改。就是嵌套里的else不能添加
winner2050 2010-09-07
  • 打赏
  • 举报
回复
就是这么折腾,遇到并发,立马错误。

还是乖乖的用数据库吧。
BoyceLyu 2010-09-07
  • 打赏
  • 举报
回复
文件权限问题吧
BaoShiqiang 2010-09-07
  • 打赏
  • 举报
回复
帮顶 对操作xml文件 不太了解
Canny 2010-09-07
  • 打赏
  • 举报
回复
太多了 我看的都头疼了等待高人了
gaeml 2010-09-07
  • 打赏
  • 举报
回复
xml文件

<?xml version="1.0" encoding="utf-8"?>
<clickrating>
<item newsid="12607" newstitle="LME6月22日铜铝官方报价" url="jinshu/12607.html" auditortime="2010-08-30 14:39" newssort="金属市场" hit="1" />
<item newsid="12600" newstitle="COMEX期铜22日收盘报价" url="jinshu/12600.html" auditortime="2010-08-30 14:39" newssort="金属市场" hit="3" />
</clickrating>

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧