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 .