62,046
社区成员
发帖
与我相关
我的任务
分享
using System.Collections.Generic;
using System.Xml;
//定义一个字典
IDictionary<string, int> dictionary = new Dictionary<string, int>();
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("xml.xml"));
XmlNodeList list = doc.GetElementsByTagName("element3");
//编历element3结点,取不同id的最大的值并放到字典中
foreach (XmlNode node in list)
{
XmlElement element = node as XmlElement;
string id = element.GetAttribute("ContentID");
int score = int.Parse(element.GetAttribute("Score"));
if (dictionary.ContainsKey(id))
{
if (dictionary[id] < score)
dictionary[id] = score;
}
else
{
dictionary[id] = score;
}
}
//将分数加起来
int result = 0;
foreach (string key in dictionary.Keys)
{
result += dictionary[key];
}
//写入xml
XmlElement element2 = doc.GetElementsByTagName("element2")[0] as XmlElement;
element2.SetAttribute("Score", result.ToString());
//保存
doc.Save(Server.MapPath("xml.xml"));