E
E
evgemiil2017-06-01 09:19:10
.NET
evgemiil, 2017-06-01 09:19:10

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

3 answer(s)
V
V Sh., 2017-06-01
@JuniorNoobie

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.

D
Dmitry Eremin, 2017-06-01
@EreminD

www.newtonsoft.com/store - the standard for working with json and xml. Use it for (de)serialization and don't suffer)

A
Andrey Golubkov, 2017-06-04
@Android97

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 question

Ask a Question

731 491 924 answers to any question