how to display xml in unity

how to display xml output in unity .i have xml document and don’t know how display that hello world in unity.by reading xml file and displaying in unity using c#

the xml code is

<?xml version="1.0" encoding="ISO-8859-1"?>

<abc>
<name>hello world</name>
</abc>

thanks in advance

You should use the WWW class to load it asynchronously.

Or, if it is one of resources being baked into you app (e.g. is within the “Resources” folder), you can access it using Resources.load:

var xml = Resources.load(_path);

… where _path is your XML file path relative to resources folder without the extension.

You should then cast it to string etc.

In more details:

using System.Collections;
using UnityEngine;

public class Test : MonoBehaviour {
	private string _url = "http://www.w3schools.com/xml/note.xml";
	private string _xml;

	IEnumerator Start() {
		 // Start a download of the given URL
		WWW www = new WWW(_url);

		// Wait for download to complete
		yield return www;

		// get text
		_xml = www.text;

		// having XML here. Do with it whatever you want...
	}

	// ... for instance, use OnGUI to render it as a label:
	void OnGUI() {
		if (null != _xml)
			GUI.Box(new Rect(10, 10, 400, 300), _xml);
		if (else)
			GUI.Label(new Rect(10, 10, 200, 30), "Loading...");
	}
}
  • attach this script to a game object
  • The script is using coroutines to hold the actual rendering until the XML is loaded

i tried this code .but im getting :error :the type of IEnumerator could not be found are u missing directives

You should put using System.Collections; on top of the file. You should definitelly look for some good C# learning resources to learn about namespaces and using commands! :slight_smile:

ps. I’ve fixed the script above.

Also, if you want to parse the XML after loading it, you can use System.Xml to do so.

Furthermore, you could use XmlSerializer to automatically convert the retrieved XML to an object structure. :slight_smile: