0
\$\begingroup\$

I need examples of how to read an xml from a server, using Unity3D..

I think it's something like this.. but I can not retrieve the information from the nodes..

An example would be of great help.. thank you..

var url = "http://server.com/test2.xml";

var www : WWW = new WWW (url); yield www;

var xml : XmlDocument = new XmlDocument();

\$\endgroup\$
1
\$\begingroup\$

In case you are using C#, you can use the XML classes from .NET. I tend to use the XPathNavigator, since most of the time you need only to read out data, and don't have to write data back.

Snippet from my actual code:

XPathDocument xmlFile;

try {
  xmlFile = new XPathDocument (filePath);
} catch (FileNotFoundException ex) {
  UnityEngine.Debug.LogException (ex);
}

// Create the navigator to navigate the file
XPathNavigator xnav = xmlFile.CreateNavigator ();


// Get the values of single nodes directly by using their XPath
string viewmode = xnav.SelectSingleNode ("/trainingSettings/viewmode").Value;
int NoItems = xnav.SelectSingleNode ("/trainingSettings/difficulty/noitems").ValueAsInt;
int NoOverallItems = xnav.SelectSingleNode ("/trainingSettings/difficulty/nooverallitems").ValueAsInt;

// Convert values using XmlConvert …
TimeSpan RemainingDurationOfTraining = XmlConvert.ToTimeSpan (xnav.SelectSingleNode ("/trainingSettings/trainingDuration").Value);

By using XPathNavigator.Select(), you can get a list of nodes back and then iterate over them, if necessary.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks for the example, I'll have to start programming in c # this part, the same code to use for Json?. \$\endgroup\$ – blese Mar 4 '14 at 20:30
  • \$\begingroup\$ Also available, System.XmlReader \$\endgroup\$ – Seth Battin Apr 4 '14 at 17:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.