Edit specific Element in XDocument

前端 未结 4 1580
萌比男神i
萌比男神i 2020-11-29 07:23

I recently started learning C# and I ran into a problem using XML.Linq to store data. I hope the question is understandable as I am not familiar with all the co

4条回答
  •  旧巷少年郎
    2020-11-29 07:35

    With using System.Xml.Linq; it becomes

     var doc = XElement.Load(fileName);
     var saveGame = doc
          .Element("savegames")
          .Elements("savegame")
          .Where(e => e.Element("IdNumber").Value == "2")
          .Single();
    
     saveGame.Element("balance").Value = "50";
    
     doc.Save(fileName);
    

提交回复
热议问题