Read single attribute from Xml and assign it to a variable.

This will sound like a very basic question, but i have very few experience with programming and even less experience with xml.
All the information I have found is overly complex as it runs trought every single node of the xml files and is somewhat related to serialization.
I just want to be able to read the value of a single attribute (int) and assing it to a previously declared variable.

  • The xml file is loaded from a web server.
  • The atribute resides inside the “weatherCode” and should be assigned to the variable weatherCode.

alt text

My current code looks like this:

alt text

Thanks in advance.

There is an amazing package in the asset store called #XML-JSON Serialization . essentially, you have to have it - it's impossible to use Unity with out it. It is likely it will solve all your problems; furthermore after you get the package if you just email the guys who make it they will help you with any very strange problems you have. You can easily find it in the Asset store, the logo looks like this: http://www.aworldforus.com/xml-json-serialization/ Note that this is the most efficient approach. it is incredibly lightweight. Really it's a marvel - why everyone uses it.

Somehow the code I posted here gets altered so i will simply repost a Screencapture. I did folowed your advice Nelledk but unity is telling me there is an error on line 21. Missing a semicolon?? I can't detect any kind of error with the code.. :/ ![alt text][1] [1]: http://i49.tinypic.com/zlc01.png

Oh, you are using javascript, i think you need to replace the first occurance of XmlReader with var in that line. i'm not completely sure if thats the error, i can try it out in an IDE tomorrow if you havent already figured it out by then :)

My mistake, I'm a newbie here sorry :)

2 Answers

2

Hi I’m the author of the XML-JSON Serialization package on the Asset Store.

With this package I would do it this way:

int weatherCode;

// Let's read the xmlString received from the server

XMLInStream stream = new XMLInStream(xmlString);

stream.Start("current_condition")
              .Content("weatherCode", out weatherCode)
          .End();

// Now weatherCode is assigned !

Thank you!

Hi

The problem is that the value you are looking for is a nested value so you atleast have to navigate to the node that you need, you could use Xpath to navigate to that exact node but i would recommend just looping over it till you find the node you need and then break as the amount of code needed is not much more and it performs better, also this way it’s easy to modify if you should need to extract more values in a future version

sorry for the wait, This code runs
you will have to test with the real xml feed yourself

I believe I understand the logic behind it now. I was expecting that if you knew the exact position of the information inside the xml file you could somehow declare the path to the nested value. Something like data/current_condition/weather_Code... As I see by your explanation the reader.ReadContent only keeps the last read value from the xml file, and we must intercept it as soon as the nested value is found.

Your expactations are right, That is Called XPath. Here is a links to an example of it's usage (the answer marked the right one) http://stackoverflow.com/questions/6209841/how-to-use-xpath-with-xdocument I would still just go with the reader solution though :)

Guys, it is unbelievably easy to do this just using a XML library. it is literally one line of code (see above ... "Content("weatherCode", out weatherCode)") It could be literally weeks of work to try to personally write code to do this. I mean you might as well start rewriting the PhysX engine or something! Heh Hope it helps some future reader

Fattie I understand using the XML library turns things much easier. However for the task at hand the explanation Nelledk has given of how to obatain the information from a node in the xml structure looks simple enough for me to understand. The only fix for now is using java instead of C#

Fattie i think you are overestimating this a bit, if it was a game that would need to manipulate xml or json data a lot then adding a library might be the better solution, but just for retriving one value from one online service it's a bit overkill as it adds a lot extra to the project. if this where to take weeks to develop, then developing a game like minesweeper would take several lifetimes :P and it took me 1 minute to write, and 5 minutes to write again with correct js syntax and test that it ran inside unity