Jump to content
  • Advertisement
Sign in to follow this  
Joker123

How to save an integer variable to an XML file?

This topic is 2253 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know how to read content from an XML file , but I don't know how to write something into itsad.png 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:

List<Gamescore> scorexml = new List<Gamescore>();
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; }
}

Share this post


Link to post
Share on other sites
Advertisement
I had a similar problem and solved it without using XML. The problem with XML is that in order to create new files (and alter the old ones) you need to perform the serialization. When using serialization you need to include some references that are not a part of the XNA Game Studio Client version, meaning that you will not be able to distribute your game to anyone who does not have the entire XNA Game Studio installed (so you can cross off both XBox and Windows phones). Another problem is that when you use serialization, you need to recompile whenever you make an alternation of the original file (and I think that is not an option).
I solved this problem by using a simple .txt file and loading it with Stream.ReadLine() method and then passing each line to some kind of processor (simple String.Split method). That way you can apply the changes to your game without recompiling it, and since Stream is a member of System.IO, it is available with the client version.

Share this post


Link to post
Share on other sites
I tried this tutorial: http://wellroundedge...gs-Manager.aspx
But I have some difficulties. The file "settings.xml" isn't getting saved. I tried it on Windows 7 but I couldn't find the filesad.png Why isn't this working?

protected override void Initialize()
{
SettingsManager.LoadSettings();
base.Initialize();
}
protected override void Update(GameTime gameTime)
{
KeyboardState kbState = Keyboard.GetState();
if (kbState.IsKeyDown(Keys.A))
SettingsManager.Settings.HasRunOnce = false;
if (SettingsManager.Settings.HasRunOnce == false)
SettingsManager.SaveSettings();
base.Update(gameTime);
}
Edited by Joker123

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!