0

I know how to read content from an XML file, but I don't know how to write something into it.

For example, I read an integer variable from an XML file and modify it. After modifying, I want to save the integer variable to the same XML file. The old integer value must be replaced with the new value. My game runs on Windows 7.

I load the XML file with this code:

protected override void LoadContent()
{
    scorexml = Content.Load<List<Gamescore>>("Score");
    foreach (Gamescore score in scorexml)
    {
        score.Load(Content);
    }   
}  


public class Gamescore
{
    int score;
    public int Score
    {
        get { return score; }
        set { score = value; }
    }
}
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Check this http://stackoverflow.com/questions/367730/how-to-change-xml-value-file-using-c-sharp If it works for you, I will post it as an answer! – Gonzalo.- Jul 16 '12 at 13:29
  • @DanielA.White That's XNA's class for loading game assets. – Tharwen Jul 16 '12 at 13:30
  • 1
    In your example you can use auto-properties to save you some typing: `public int Score { get; set; }` – Tim S. Jul 16 '12 at 13:45

1 Answers1

0

I can suggest you two approaches.

  1. Using a class that represent your XML and that implements the ISerializable interface:
    • deserialize the xml into an object, instance of the class.
    • process the data (setting integer values or whatever).
    • serialize the object into the xml file.
  2. Using Linq to XML to process the XML:
    • create a XDocument from xml file.
    • process the XDocument with Linq.
    • save the XDocument into the xml file.
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
  • I tried this tutorial: http://wellroundedgeek.com/post/2011/01/25/Simple-XNA-Cross-Platform-Settings-Manager.aspx – Homer_Simpson Jul 20 '12 at 21:35
  • @Homer_Simpson, I'm not sure the best way to report a broken link so before deleting it or flagging it (No option in flags say broken) I figured I'd let you know in case you had an updated link. – Carl- Sep 26 '19 at 16:54