/// <summary> /// 修改某一首歌的节点值 /// </summary> /// <param name="Title">标题</param> /// <param name="ElementName">节点名</param> /// <param name="ElementValue">修改值</param> public void ModifyMusic(string Title, string ElementName, int ElementValue) { //保存原始的节点值 string Node_Title = string.Empty; string Node_Url = string.Empty; string Node_Type = string.Empty; string Node_Like = string.Empty; string Node_Listen = string.Empty; //查询 XElement Xe = XElement.Load(XmlFilePath); IEnumerable <XElement> Elements = from Musics in Xe.Elements("Music") where Musics.Element("Title").Value == Title select Musics; //得到原始的值 if (Elements.Count() > 0) { foreach (XElement E in Elements) { Node_Title = E.Element("Title").Value; Node_Url = E.Element("Url").Value; Node_Type = E.Element("Type").Value; Node_Like = E.Element("Like").Value; Node_Listen = E.Element("Listen").Value; } } //修改 XElement XEl = Elements.First(); //确定是修改Listen还是很Like节点 if (ElementName == "Listen") { //修改Listen的值 int ListenValue = Convert.ToInt32(Node_Listen) + ElementValue; XEl.ReplaceNodes( new XElement("Title", Node_Title), new XElement("Url", Node_Url), new XElement("Type", Node_Type), new XElement("Like", Node_Like), new XElement("Listen", ListenValue)); Xe.Save(XmlFilePath); } else { if (ElementName == "Like") { int LikeValue = Convert.ToInt32(Node_Like) + ElementValue; XEl.ReplaceNodes( new XElement("Title", Node_Title), new XElement("Url", Node_Url), new XElement("Type", Node_Type), new XElement("Like", LikeValue), new XElement("Listen", Node_Listen)); Xe.Save(XmlFilePath); } } }
/// <summary> /// 返回指定歌曲名称的Music /// </summary> /// <returns></returns> public Music GetMusic(string Title) { Music m = null;; //加载文件 XElement Xe = XElement.Load(this.XmlFilePath); //读取列表 IEnumerable <XElement> Elements = from Musics in Xe.Elements("Music") where Musics.Element("Title").Value == Title select Musics; foreach (XElement Element in Elements) { m = new Music(Element.Element("Title").Value, Element.Element("Url").Value, Convert.ToInt32(Element.Element("Like").Value), Convert.ToInt32(Element.Element("Listen").Value), GetTypeByValue(Element.Element("Type").Value)); } return(m); }
/// <summary> /// 删除根据满足某个节点值条件的歌曲 /// </summary> /// <param name="Value"></param> public void DeleteMusic(string ElementName, string ElementValue) { XElement Xe = XElement.Load(XmlFilePath); IEnumerable <XElement> Elements = from Musics in Xe.Elements("Music") where Musics.Element(ElementName).Value == ElementValue select Musics; if (Elements.Count() > 0) { Elements.First().Remove(); } Xe.Save(XmlFilePath); }