Answer the question
In order to leave comments, you need to log in
How to make a record to be added and not replaced?
the code should add an entry to the statistics, and it replaces, here is the full project in the github https://github.com/evgeniel/XO.git as I understand it because data = Serializer.GetData(filePath); stands after Serializer.SetData(filePath, data); but this GetData function returns a value, if you put it first, the code stops on it, in general, help me figure it out
public void SaveStat() //сохранение статистики
{
try
{
var data = new List<Statistcs>();
string filePath = "stats.xml";
data.Add(new Statistcs()
{
Date = DateTime.Now,
Result = state,
StepCounter = stepCounter,
UserFirst = true
});
Serializer.SetData(filePath, data);
data = Serializer.GetData(filePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Answer the question
In order to leave comments, you need to log in
What exactly does the SetData function do? If it completely overwrites the file with new data, then put the line "data = Serializer.GetData(filePath)" right after the file path declaration. If it does not rewrite, but only adds, then everything should work fine.
www.newtonsoft.com/store - the standard for working with json and xml. Use it for (de)serialization and don't suffer)
Try like this:
public void SaveStat() //сохранение статистики
{
try
{
var data = new List<Statistcs>();
string filePath = "stats.xml";
data = Serializer.GetData(filePath);
data.Add(new Statistcs()
{
Date = DateTime.Now,
Result = state,
StepCounter = stepCounter,
UserFirst = true
});
Serializer.SetData(filePath, data);
data = Serializer.GetData(filePath);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question