实现功能:统计点击量,并更新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;
        }

解决方案 »

  1.   

    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>
      

  2.   

    不是权限问题的,第一个if里的能添加成功,嵌套里的if也可以修改。就是嵌套里的else不能添加
      

  3.   

    把else换下
    if(true)哈哈,开个玩笑
      

  4.   

    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( "", "");