Hello to all. I have an xml file of this format:

<?xml version="1.0" encoding="utf-8"?> <locations> <location id="1"> <level name="3" complete="True" stars="2" firstMisson="True" secondMission="False" thridMission="False" /> </location> <location id="2"> <level name="4" complete="True" stars="3" firstMisson="True" secondMission="True" thridMission="True" /> </location> </locations> 

Tell me how to get the data to be used in the future.

Well, sort of figured out the first item.

Created classes from xml

  [System.Serializable] [XmlRoot(ElementName = "level")] public class Level { [XmlAttribute(AttributeName = "name")] public string Name { get; set; } [XmlAttribute(AttributeName = "complete")] public string Complete { get; set; } [XmlAttribute(AttributeName = "stars")] public string Stars { get; set; } [XmlAttribute(AttributeName = "firstMisson")] public string FirstMisson { get; set; } [XmlAttribute(AttributeName = "secondMission")] public string SecondMission { get; set; } [XmlAttribute(AttributeName = "thridMission")] public string ThridMission { get; set; } } [System.Serializable] [XmlRoot(ElementName = "location")] public class Location { [XmlElement(ElementName = "level")] public Level Level { get; set; } [XmlAttribute(AttributeName = "id")] public string Id { get; set; } } [System.Serializable] [XmlRoot(ElementName = "locations")] public class Locations { [XmlElement(ElementName = "location")] public Location Location { get; set; } public List<Locations> LocDb = new List<Locations>(); } [System.Serializable] [XmlRoot(ElementName = "xml")] public class Xml { [XmlElement(ElementName = "locations")] public Locations Locations { get; set; } } 

Here is a method for deserialization.

  public List<Locations> locDB = new List<Locations>(); public static void LoadData() { string filepath = Application.dataPath + @"/XML/GameXMLdata.xml"; var xmlSerializer = new XmlSerializer(locDB.GetType()); var stream = File.Open(filepath, FileMode.Open); locDB = (List<Locations>)xmlSerializer.Deserialize(stream); // На эту строку ругается //<locations xmlns=''> was not expected stream.Close(); Debug.Log(locDB[1].Location.Id); } 

I want to drive the data to the list from which to load them. Tell me what cant. How best to gash to make it easier.

  • You need to read about the different methods and ways of reading XML in c #. Because there are a lot of them and different, to describe them all - Alexey Shimansky
  • Alternatively, you can use XML serialization. For this, objects are created that describe the structure of your data and through it are driven into XML data. On Monday from work I will throw off the version as I did. The result is very fast saving, loading and working with data as objects. - KingPeas

2 answers 2

You need to use deserialization.

System.Xml.Serialization will help you with this and specifically XmlSerializer.Deserialize() , you need to bring some preparatory work.

To do this, bring your xml file to this form (in the future you can return all parameters and their values ​​removed from the opening tag):

 <xml> <locations> <location id="1"> <level name="3" complete="True" stars="2" firstMisson="True" secondMission="False" thridMission="False" /> </location> <location id="2"> <level name="4" complete="True" stars="3" firstMisson="True" secondMission="True" thridMission="True" /> </location> </locations> </xml> 

Then copy this content to the clipboard. In visual studio there is a very convenient feature, about which for some reason not many people know. When working with json or xml, there is no need to create a class for serialization manually. You need to create a new class file and delete absolutely all lines from it, then you just need to go through the path:

 Правка(Edit) => Специальная вставка(special paste) => Вставить XML как классы(Paste XML as classes) 

And the class for working with this XML will be created automatically.

Then you deserialize your XML and work with the object.

More details can be found here: https://msdn.microsoft.com/ru-ru/library/tz8csy73(v=vs.110).aspx

As it was said in the comments of the decisions can be really set. In my opinion this is one of the most convenient.

    Method 1:

    Make everything handles using LINQ TO XML and get the necessary data, which you then load into variables. Using the namespace System.Xml.Linq .

    Method 2:

    Through nested loops, bypass XML nodes and write data to variables. Using the System.Xml namespace .

    XML Guide in Russian.

    Method 3:

    Creates a class based on XML. When XML is loaded, it is de-serialized to the class type and .NET automatically parses everything and returns the filled object.

    Guide to serialization / deserialization in Russian.