62,271
社区成员
发帖
与我相关
我的任务
分享
[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;
}
<?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>