Good evening is not good at xml, but I need it.

<global> <level-1> <gamer name="ilya"/> <score id="1" /> </level-1> </global> 

An example from the top. So I need to read the number 1 which is in id using C # and write to the variable z . I can’t do this for an hour, but I still don’t know.

Closed due to the fact that the question is too common for participants Streletz , aleksandr barakin , Cerbo , rdorn , D-side 2 Jun '16 at 16:01 .

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 .

  • uh ... use any xml-парсер in C# even embedded somewhere was. - pavel
  • But can you try? - Ilya Vityuk
  • 2
    @ Ilyavituk and something you "must"? I’ll try to throw in now, but most likely you are doing something wrong, since you have such a problem. - Monk
  • one
    @ Ilyavityuk, and what specifically need help? What exactly is the problem? That does not work? Your code can be seen? - Streletz
  • one
    > Damn instead of criticizing and putting-could already help !!! - and you could answer the questions of those from whom you ask for advice. - Monk

1 answer 1

  var doc = XDocument.Load("person.xml"); var score = doc.Descendants().FirstOrDefault(d => d.Name == "score"); var id = score.Attributes().Single(a => a.Name == "id").Value; 

Downloading a file, searching for an element called score and reading an attribute from it, called id . In the current form, the received id is a string, what type of z have you not specified, so in general the problem is solved.

I looked at your questions - apparently, you want to work with game statistics.

So, the above code is very inconvenient for this.

What would I roughly suggest. First, the auxiliary code for working with xml:

  class Serializer<T> { /// <summary> /// Сохранить в файл. /// </summary> public static void Save(string path, T data) { var formatter = new XmlSerializer(typeof(T)); using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write)) formatter.Serialize(stream, data); } /// <summary> /// Загрузить из файла. /// </summary> public static T Load(string path) { var type = typeof(T); T retVal; var formatter = new XmlSerializer(type); try { using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) retVal = (T)formatter.Deserialize(stream); } catch (System.Exception) { return default(T); } return retVal; } } 

Now, using this class, you can easily write such classes for statistics:

  public class PlayerStatistic { public string Player { get; set; } public int Score { get; set; } public string LevelName { get; set; } public int LevelId { get; set; } } public class GameStatistic { private static readonly string XmlFile = "GameStatistic.xml"; public List<PlayerStatistic> PlayerStatistics { get; set; } public void Save() { Serializer<GameStatistic>.Save(XmlFile, this); } public static GameStatistic Load() { return Serializer<GameStatistic>.Load(XmlFile) ?? new GameStatistic(); } private GameStatistic() { this.PlayerStatistics = new List<PlayerStatistic>(); } } 

Well, how to use it in the game:

  var gameStat = GameStatistic.Load(); foreach (var statistic in gameStat.PlayerStatistics) { // делаете что хотите с каждой записью statistic. } // Добавление новой записи var newResult = new PlayerStatistic(); newResult.Player = "Me!"; newResult.Score = 123; newResult.LevelName = "Tutorial"; newResult.LevelId = 1; gameStat.PlayerStatistics.Add(newResult); // Не забываем сохранять статистику в файл. gameStat.Save(); 

The GameStatistic class in the current implementation is better to load only one, so as not to lose statistics. Usually, the loner pattern is used for this, you can google it if you need it.

This is what the xml looks like:

 <?xml version="1.0"?> <GameStatistic xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <PlayerStatistics> <PlayerStatistic> <Player>Me!</Player> <Score>123</Score> <LevelName>Tutorial</LevelName> <LevelId>1</LevelId> </PlayerStatistic> <PlayerStatistic> <Player>Me!</Player> <Score>222</Score> <LevelName>First Step</LevelName> <LevelId>2</LevelId> </PlayerStatistic> </PlayerStatistics> </GameStatistic> 

The main thing that is important for you is to determine in advance what will be in the statistics. I, looking at your xml - threw the player there, the score, the name of the level and some potential id of the level, the name to display, and id to unambiguously group the data, you never know, the levels will be renamed.

PS: in general, the code supports the smooth addition of properties in both PlayerStatistic and GameStatistic .

  • "I looked at your questions - apparently, you want ..." - this is the desire to help! I respect you. @ Ilyavityuk - appreciate. - Igor