sorry
tell you about the file there is the location?
something like that ?
if (!File.Exists(_fileName))
Directory.CreateDirectory(_fileName);
If(!File.Exists($ ‘’{_fileName}/test.xml’’));
File.CreateFile ($ ‘’{_fileName}/test.xml’’));
sorry
tell you about the file there is the location?
something like that ?
if (!File.Exists(_fileName))
Directory.CreateDirectory(_fileName);
If(!File.Exists($ ‘’{_fileName}/test.xml’’));
File.CreateFile ($ ‘’{_fileName}/test.xml’’));
in my case I check the repeater I do not check the file the file I create later in the repeater XmlSerializer xs = new XmlSerializer (typeof (List )); // Write in an XML file using (StreamWriter sw = new StreamWriter ($ “{_ fileName} /test.xml”)) { xs.Serialize (sw, HighScores); }
I placing in Game1
EDIT
I am really afraid to ask which class now…
sorry I did not understand?
did I upset you? if that’s the case that’s not my goal at all i’m really sorry
Let’s try again, where in your code are you using the code to check for the XML file[s]…
Which class, which part of XNA/MG…
It might just be best for you to post your entire code at this point…
Please use the Code input in the forum entry area to format your code correctly as well, not in bizarre fragments… simply paste your code, highlight it all and click the code button…
That button. And ensure you are posting formatted code… we’re here to help you, not do your homework for you.
Was supposed to be these icons, was using my phone that time lol
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using Android.Content.Res;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace processXml
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private SpriteFont _font;
private long _score=0;
private float _timer;
private Random random;
public static string _fileName;
private List<Score> _highScores { get; set; }
private List<Score> _scores { get; set; }
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
random = new Random();
//folder android
_fileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (!Directory.Exists(_fileName))
Directory.CreateDirectory(_fileName);
if (!File.Exists($"{_fileName}/test.xml"))
File.Create(($"{_fileName}/test.xml"));
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
_font = Content.Load<SpriteFont>("Font");
_timer = 5;
ReadXml();
}
//read xml
public void ReadXml()
{
XmlSerializer xs = new XmlSerializer(typeof(List<Score>));
//Read file XML
using (StreamReader sr = new StreamReader($"{_fileName}/test.xml"))
{
_scores = (List<Score>)xs.Deserialize(sr);
}
}
//write xml
public void WriteXml()
{
//use serializer XML
XmlSerializer xs = new XmlSerializer(typeof(List<Score>));
using (StreamWriter sw = new StreamWriter($"{_fileName}/test.xml"))
{
xs.Serialize(sw, _scores);
}
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
_score = random.Next(0, 100); // Returns a random number from 0-99
_timer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
// If the round is over
if (_timer <= 0)
{
_scores = new List<Score>
{
new Score("Player",_score),
new Score("Player2",_score +10)
};
_scores = _scores.OrderByDescending(c => c.Value).ToList();
_timer = 5;
// _score = 0;
WriteXml();
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
_spriteBatch.DrawString(_font, "Score: " + _score, new Vector2(10, 10), Color.Red);
_spriteBatch.DrawString(_font, "Time: " + _timer.ToString("N2"), new Vector2(10, 30), Color.Red);
// _spriteBatch.DrawString(_font, "Highscores:\n" + string.Join("\n", _scores.Select(c => c.PlayerName + ": " + c.Value).ToArray()), new Vector2(50, 50), Color.Red);
int i = 0;
foreach(Score score in _scores)
{
_spriteBatch.DrawString(_font, $"{score.PlayerName}: {score.Value}", new Vector2(8, i++ * _font.LineSpacing), Color.White);
}
//show android folder
//_spriteBatch.DrawString(_font, "" + _fileName ,new Vector2(0, 0), Color.White);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
//Class Score
using System;
namespace processXml
{
[Serializable]
public class Score
{
public string PlayerName { get; set; }
public long Value { get; set; }
public Score ()
{
}
//constructor
public Score (string playername, long value )
{
PlayerName = playername;
Value = value;
}
}
}
I think I did a bad manipulation
is it this </> button?
Oh god, I was waiting for someone else to say this, please refrain from posting multiple times, use the edit button.
the I just erased is it good?
OK
how i can properly format the code please?
I did not understand very well
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using Android.Content.Res;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace processXml
{
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
private SpriteFont _font;
private long _score=0;
private float _timer;
private Random random;
public static string _fileName;
private List<Score> _highScores { get; set; }
private List<Score> _scores { get; set; }
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
random = new Random();
//folder android
_fileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (!Directory.Exists(_fileName))
Directory.CreateDirectory(_fileName);
if (!File.Exists($"{_fileName}/test.xml"))
File.Create(($"{_fileName}/test.xml"));
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
_font = Content.Load<SpriteFont>("Font");
_timer = 5;
ReadXml();
}
//read xml
public void ReadXml()
{
XmlSerializer xs = new XmlSerializer(typeof(List<Score>));
//Read file XML
using (StreamReader sr = new StreamReader($"{_fileName}/test.xml"))
{
_scores = (List<Score>)xs.Deserialize(sr);
}
}
//write xml
public void WriteXml()
{
//use serializer XML
XmlSerializer xs = new XmlSerializer(typeof(List<Score>));
using (StreamWriter sw = new StreamWriter($"{_fileName}/test.xml"))
{
xs.Serialize(sw, _scores);
}
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
_score = random.Next(0, 100); // Returns a random number from 0-99
_timer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
// If the round is over
if (_timer <= 0)
{
_scores = new List<Score>
{
new Score("Player",_score),
new Score("Player2",_score +10)
};
_scores = _scores.OrderByDescending(c => c.Value).ToList();
_timer = 5;
// _score = 0;
WriteXml();
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied);
_spriteBatch.DrawString(_font, "Score: " + _score, new Vector2(10, 10), Color.Red);
_spriteBatch.DrawString(_font, "Time: " + _timer.ToString("N2"), new Vector2(10, 30), Color.Red);
// _spriteBatch.DrawString(_font, "Highscores:\n" + string.Join("\n", _scores.Select(c => c.PlayerName + ": " + c.Value).ToArray()), new Vector2(50, 50), Color.Red);
int i = 0;
foreach(Score score in _scores)
{
_spriteBatch.DrawString(_font, $"{score.PlayerName}: {score.Value}", new Vector2(8, i++ * _font.LineSpacing), Color.White);
}
//show android folder
//_spriteBatch.DrawString(_font, "" + _fileName ,new Vector2(0, 0), Color.White);
_spriteBatch.End();
base.Draw(gameTime);
}
}
}
//Class Score
using System;
namespace processXml
{
[Serializable]
public class Score
{
public string PlayerName { get; set; }
public long Value { get; set; }
public Score ()
{
}
//constructor
public Score (string playername, long value )
{
PlayerName = playername;
Value = value;
}
}
}
Great
Hi guys a question please there is no other simpler way to meet my expectations not necessarily xml you haven’t encountered this kind of situation? I just want to save the scores then display the best score Thanks for your help
You could just use a plain text file…
hi,
thank you
can you enlighten me more please?
You know, the point of a forum isn’t actually, ‘Give me the codes!’ I feel like that is your expectation here.
Anyway, here’s a pointer:
it’s not my goal and i never asked for that
I’ve been looking for a solution for over a month
Thanks for your help