Home c# Getting data from XML Unity

Getting data from XML Unity

Author

Date

Category

Hello everyone. I have an XML file of this format:

& lt; xml version = "1.0" encoding = "UTF-8"? & gt;
  & lt; locations & gt;
    & lt; location id = "1" & gt;
      & lt; Level Name = "3" Complete = "True" Stars = "2" firstMisson = "True" secondmission = "false" thritmission = "false" / & gt;
    & lt; / location & gt;
    & lt; location id = "2" & gt;
      & lt; level name = "4" Complete = "True" Stars = "3" firstmisson = "true" secondmission = "true" thritmission = "True" / & gt;
    & lt; / location & gt;
  & lt; / locations & gt;

Tell me how to get the data that is later used.

Well, it seems like the first item figured out.

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 & lt; locations & gt; LOCDB = NEW LIST & LT; Locations & gt; ();
  }
  [System.Serializable]
  [Xmlroot (ElementName = "XML")]
  Public Class Xml.
  {
    [XMLelement (ElementName = "Locations")]
    Public Locations locations {get; SET; }
  }

Here is the deserialization method

public list & lt; locations & gt; LOCDB = NEW LIST & LT; Locations & gt; ();
   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 & lt; locations & gt;) xmlserializer.deserialize (stream); // swear on this line
      // & lt; locations xmlns = '' & gt; Was Not Expected.
      stream.close ();
      Debug.log (LOCDB [1] .location.id);
     }

I want the data to drive into the List from which and to gain them. Tell me what a cant. How best to install to make it easier to hide.


Answer 1, Authority 100%

You need to use deserialization.

This will help you SYSTEM.XML.SERIALIZATION and specifically xmlserializer.deserialize () , you must bring some preparatory work.

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

& lt; xml & gt;
& lt; locations & gt;
  & lt; location id = "1" & gt;
    & lt; Level Name = "3" Complete = "True" Stars = "2" firstMisson = "True" secondmission = "false" thritmission = "false" / & gt;
  & lt; / location & gt;
  & lt; location id = "2" & gt;
    & lt; level name = "4" Complete = "True" Stars = "3" firstmisson = "true" secondmission = "true" thritmission = "True" / & gt;
  & lt; / location & gt;
& lt; / locations & gt;
& lt; / xml & gt;

Then copy this content to the clipboard. Visual Studio has a very comfortable chip, about which for some reason not many know. When working with JSON or XML, there is no need to create a manual serialization class. You need to create a new class file and remove absolutely all lines from it, then you just need to go along the way:

edit (edit) = & gt; Special Insertion (Special Paste) = & gt; Insert XML as Classes (Paste Xml As Classes)

and class to work with this XML will be created automatically.

Next, you deserialize your XML and work already with the object.

You can read more detail here:
https://msdn.microsoft.com/en RU / Library / TZ8CSY73 (V = VS.110) .aspx

As it was said in the Comments, solutions can be really many. In my opinion this is one of the comfortable.


Answer 2, Authority 50%

Method 1:

Make all the handles using LINQ TO XML and get the necessary data that then downloaded to variables. Using the namespace System.xml.linq .

2:

Through the invested cycles bypass XML nodes and write data to variables. Using namespace System.xml .

XML Guide in Russian.

3:

Class is created based on XML. When the XML is loaded, it is desperilized to the class and .NET type automatically all breaks and return the completed object.

Serialization / Reseserial Guidelines in Russian.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions