实现功能:统计点击量,并更新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;
}
现在问题:在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;
}
解决方案 »
- 网站后台管理
- 页面指示灯提示问题。。。。。。。急急急
- asp.net开发,数据录入大家都用什么表格控件?
- CheckBoxList如何获取他的DataValueField的值?
- 免费的visual web developer express是不是不能编译代码啊?
- 看别人的代码,经常会看到“Utility”一词,不是很理解,请大家解释,谢谢!
- 用jmail4.4发送邮件附件存在的问题?100分
- 如果编写这样的自定义控件?(高手很简单)
- 关于ultrawebgrid的问题
- 如何产生一个不规则的datagrid??好像有点难度!!!
- 帮忙个看下!!!!Internet explorer 无法打开Internet站点
- 怎么获得这个function里面 <input type="text"> 里面的值?
<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>
if(true)哈哈,开个玩笑
通过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( "", "");