-1

I have the following xml code (Highscores.xml):

<highscore>
  <score>
    <name>Pipo</name>
    <points>200</points>
  </score>
</highscore>

I have a textbox where the player need to write his name to save it. Also the points need to be saved. How can I add these two items to my xml file?

PythonIITD
  • 85
  • 8

2 Answers2

2
    XmlDocument doc = new XmlDocument();
    doc.Load(@"D:\Highscores.xml");
    var name = doc.SelectSingleNode("/highscore/score/name");
    if (name != null)
        name.InnerXml = "ojlovecd";
    var points = doc.SelectSingleNode("/highscore/score/points");
    if (points != null)
        points.InnerXml = "12345";
    doc.Save(@"D:\Highscores.xml");
ojlovecd
  • 4,812
  • 1
  • 20
  • 22
0

Here's a LINQ version for completeness:

   XDocument xDoc = XDocument.Load(@"C:\OldFile.xml");
   var score = xDoc.Element("highscore").Element("score");

   score.Element("name").Value = "NewName";
   score.Element("points").Value = "100";

   xDoc.Save(@"C:\NewFile.xml");

As always, null-check your variables before trying to do anything with them.

ry8806
  • 2,258
  • 1
  • 23
  • 32