How to get XML from a URL?

All the tutorials show how to load the XML from the resources folder and that is working fine using:

TextAsset menuXML = Resources.Load<TextAsset>("GodWallpaperMenu");
XmlDocument itemDataXml = new XmlDocument();
itemDataXml.LoadXml(menuXML.text);

But how do I get it from a url? I tried using WWW then doing:
itemDataXml.LoadXml(www.text);:wink:
but everything I try to pass into itemDataXml.LoadXml returns ā€œData at root level is invalid.ā€ The XML is the same on the website that it is in the resources folder.

I did look at that but it doesnā€™t tell me how to turn the result into xml which is where Iā€™m stuck. Here what Iā€™m tryingā€¦

using (UnityWebRequest request = UnityWebRequest.Get("http://www.showandtelltechnology.com/AssetBundles/GodWallpaperMenu.xml"))
        {
            yield return request.SendWebRequest();

            if (request.isNetworkError) // Error
            {
                Debug.Log(request.error);
            }
            else // Success
            {
                Debug.Log(request.downloadHandler.text);
               
                itemDataXml.LoadXml(request.downloadHandler.text);
            }
        }

XML is just text.

Start with the actual payload data. Thatā€™s probably what is broken.

Make an extremely tiny XML document and serve it.

Print out the payload. What does it look like to you?

Count the characters. Did they all make it? Did any extra characters get served?

etc
etc
etc

Just standard old Debugging 101, nothing to see here, move along.

If you think it actually IS a networking problem, start here:

Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

https://discussions.unity.com/t/831681/2

https://discussions.unity.com/t/837672/2

And setting up a proxy can be very helpful too, in order to compare traffic:

https://support.unity.com/hc/en-us/articles/115002917683-Using-Charles-Proxy-with-Unity

So stupid. The code is fine. I opened my xml file on my web server and it had a strange character at the beginning. I donā€™t know where that came from. I removed it and everything works.

Very likely a BOMā€¦ responsible for literally MILLIONS of hours of collective wasted time tracking them down.

https://en.wikipedia.org/wiki/Byte_order_mark

Yet another disaster that befell computer science when we foolishly moved beyond ASCII.

1 Like