I created a classic C # snake game with score levels, etc., but I wanted to make a high score table and I can't do it, because prompted to make an xml, but I never worked with him.

How can this be implemented? Namely, it is necessary to create on 5 fields where the name and account will be written and in the presence of a larger account, replace the name and account.

Closed due to the fact that the question is too common for the participants Streletz , Grundy , D-side , aleksandr barakin , Nick Volynkin Jun 3 '16 at 6:17 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • I would use sqllite or json. XML is still like a cannon on a sparrow. - Dmitry Chistik
  • Well, I wouldn’t say so, of course, maybe so, but anyway it will be more convenient for me on xml (yes, I’m such a person) - Ilya Vityuk
  • Well, if XML, then Habré is a very good article. Much depends on your requirements, which will grow with the toy development process. The exact code or example you can tell here if you lay out the structure of the records of your program - Dmitry Chistik
  • The article is very entertaining, but there is nothing unspeakably how to replace - Ilya Vityuk
  • It was part 1 and can I find part 2? - Ilya Vityuk

2 answers 2

Here is my JSON example, if it suits you, of course, it uses JavaScriptSerializer !! requires System.Web.Extensions !!

 //Ваш класс записи рекорда public class MyRecord { public string Name { get; set;} public int Record { get; set;} } 

How to write a sheet of records in a text variable

 //Запись в текстовую переменную forWriteToFile List<MyRecord> allRecords = new List<MyRecord>(); ... //Тут заполнение allRecords ... System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); string forWriteToFile = serializer.Serialize(allRecords ); 

How to read a sheet of records from a text variable

 //Чтение из текстовой переменной forWriteToList System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); List<MyRecord> allRecords = serializer.DeserializeObject<List<MyRecord>>(forWriteToList); 

UPD: Editing Example

 string JSON = Прочитать_Всё_Из_Файла("Рекорды.txt");//тут прочитали из файла в текстовую переменную JSON System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); List<MyRecord> allRecords = serializer.Deserialize<List<MyRecord>>(JSON); allRecords.RemoveAt(1);//Удалили вторую запись foreach(MyRecord record in allRecords) record.Record++;//Увеличили все рекорды на единицу allRecords.Add(new MyRecord(){Name="Вася", Record=1000});//Добавили запись JSON = serializer.Serialize(allRecords); //Записали обратно в переменную JSON Записать_В_Файл("Рекорды.txt", JSON);//Далее напишите запись текста в файл 
  • How to change? - Ilya Vityuk
  • Read, change or delete or add your records to the sheet and write down. Namnooooogo easier XML - Dmitry Chistik
  • Well, if XML, then Habré is a very good article. Much depends on your requirements, which will grow with the toy development process. The exact code or example you can tell here if you post the structure of the records of your program - Dmitry Chistik The article is very entertaining, but there is no way to replace it - Ilya Vityuk This was part 1 and can you find part 2? - Ilya Vityuk

First you have to get a class or structure for storing information about the player, for example:

 struct PlayerInfo { public string Name; public int Score; } 

In the simplest case, there will be enough structure, but it can be transformed at any time into a full-fledged class, and at the same time we can figure out how the .NET structures differ from the classes (they have already answered this question more than once).

Next, create a repository for our records,

 List<PlayerInfo>() recordTable = new List<PlayerInfo>(); 

Perfect for this purpose.

In the course of the program, we edit our List as a normal array, at the beginning of the program we load values ​​from the file into it, at the end we upload to the file.

For storage on disk, a simple text format is suitable in which one line stores information about one player, and the values ​​are separated by a semicolon, a la .csv , for example: "Вася Пупкин;100500" . Subsequently, you can modify to a full. .csv or more complex format. You should not immediately take up XML, and for such a simple case it is clearly redundant, or the author does not agree on something =)

It remains to implement the preservation and loading of the table of records, in the proposed version with a text format, everything is done line by line reading and writing to the file, how to do it in the official documentation on MSDN, on this and many other resources, therefore, without details.

For convenience, you can add methods to the structure / class for convenient conversion of values:

 public void SetFromCsv(string csvStr) { string[] fields = csvStr.Split(';'); Name = fields[0]; Score = int.Parse(fields[1]); } public string GetCsvString() { return string.Format("{0};{1}", Name, Score); } 

If you still really want XML, then look towards using XDocument and / or XElement . They have built-in tools for loading and saving xml files simply by specifying the full name of the desired file. However, I note that sorting the nodes inside XElement.Nodes , unlike the List , cannot be accomplished with a single line of code, only by hand, deleting and creating the nodes in the required order. Therefore, it makes sense to use XML only for storage and loading, and all other actions to be performed on the List .

  • Well, you need to create a sheet or array of 10 and so that you can replace them if necessary + rdorn correctly says that I do not finish it. OK. I just gave the dad a task so that I did it on xml. so i need to xml. - Ilya Vityuk
  • @ Ilyavityuk then can first ask dad to tell something about XML and serialization, since this is his idea, and then try to do something based on new knowledge? Because a ready-made solution will certainly work, but you will not understand it, but, alas, it doesn’t increase from a blind copy-paste of knowledge - rdorn