Accessing XML through www

Hi!

I’m looking for a quick solution to accessing an XML file through a WWW request.

My WWW request returns the text of an XML, and i’d want to use it without having it distorted(I’ll explain distortion below).

	WWW www = new WWW(url);				
	while(!www.isDone)
	{			
		yield return new WaitForSeconds(0.1f);
	}
	
	string wwwtext = ASCIIEncoding.ASCII.GetString(Encoding.Convert(Encoding.UTF8,Encoding.ASCII,www.bytes));	
	
	Debug.Log("new xmldoc");		
	XmlDocument result = new XmlDocument();
	
	
	Debug.Log("loadxml");		
	try
	{
		result.LoadXml(wwwtext); 
	}
	catch{}		

	
	Debug.Log(wwwtext);	
	
	Debug.Log(result.InnerXml.ToString());
	return false;

So here, i get the xml text which can’t be parsed to the XmlDocument, giving me an exception.
When I print the text i get back from the WWW, it’s different from the XML source, for example:

<resp stat="ok" version="2.0">

became

{"resp": {"status": true, "search":

What can I do to get the original xml source?

Thanks for the help!

Um, It doesn’t look like the server you are trying to get xml from is giving you xml.

The format that looks like what you have:
{“resp”: {“status”: true, “search”: ect
is called

[1] a lot of people are using it now a days instead of XML since the files tend to be much smaller and faster to send over the internet. 

If it is your own server that you are trying to get data from, try and figure out why your framework is giving you JSON and not XML. 

If it is not your server or if you can't figure it out, its not a big deal either, JSON tends to be easier to work with the XML anyway you just need to find a JSON library to help you get at the data. 

I would recommend [litJSON][2], its a nice simple open source .net JSON library that works very well with unity. Just drop the dll in your project read through its docs a bit and your should be good to go.

I have a lot of experience loading both XML and JSON data into Unity, but I know relatively little of the server side aspects of it (I usually pay someone to setup the web api for me) so I will not be of much help in try to help you configure your server if that is the problem. 


  [1]: http://en.wikipedia.org/wiki/JSON
  [2]: http://litjson.sourceforge.net/