[C# Scripting] - Read a XML Valu

Hi.

I was able to get the value of xml into a variable, but now I’d like to simply get the nodes by name and display in the log.

I have that content:

<?xml version="1.0" encoding="ISO-8859-1"?>
<enemys>
  <node>
    <name>Test 1</name>
  </node>
  <node>
    <name>Teste 2</name>
  </node>
</enemys>

Into that variable:

public string myXMLReceived;

Thank You

If you’re using an XMLTextReader like the article I linked to in your last thread then you can traverse the graph more easily if you use the NodeType property like this:

List<string> names = new List<String>();
while(reader.Read())
{
    if(reader.NodeType == XMLNodeType.Text)
    {
        names.Add(reader.Value);
    }
}

Otherwise you will have to do some more complex string parsing, iterating through it looking for occurences of “”, then get the substring from that to the next occurence of “”.

Honestly it’ll be easier using the first method because the second is clunky, hard to debug + maintain and gets far more complex when you have to take parent nodes into account.

Why not just use JSON instead?

It’s faster, simpler and does pretty much everything a game developer ever needs. There’s tons of parsers available, many of which consume only fraction of the space .Net XML libraries do. I’d even go as far as claiming that it’s the preferred format of Game Developers and software developers in general.

Wrote a tutorial a while back about saving data to JSON using the one file wonder SimpleJSON. There’s also Json.Net which is fairly popular among Unity developers.