- Home /
Download multiple files/images read from xml
Hi. I have a problem downloading multiple images. I have a xml file with tags which hold URLs to the images I want to download (or load from local hard drive with file:// prefixes). There is no problem in obtaining the correct URLs, so this is no issue. I have the following method to download the images:
IEnumerator downloadImages(XmlNodeList imageNodes)
{
for(int i=0; i< imageNodes.Count; i++)
{
XmlNode node = imageNodes.Item (i);
using(WWW www = new WWW(node.InnerText))
{
yield return www;
Texture2D texture = new Texture2D(1, 1);
www.LoadImageIntoTexture (texture);
Sprite sprite = Sprite.Create (texture, new Rect (0, 0, texture.width, texture.height), new Vector2 (0.5f, 0.5f));
imageDict.Add (node.InnerText, sprite);
}
}
}
If there is only one image/image URL, there is no problem. But anything more than 1 throws "Couldn't open file url-of-file". To make sure that there is nothing wrong with the URL or the files itself, I changed the order of them in the xml file. Whatever comes first will be downloaded and created correctly, the second try will end with the mentioned error.
I also tried calling #Dispose() on the www object manually, it did not help. And yes, I am calling this with #StartCoroutine(...).
Any hint is appreciated.
Answer by Dantel35 · Aug 12, 2015 at 08:44 AM
I figured it out. The code shown here works fine. It was the XmlReaders fault after all. I am using System.Xml to load an xml File like this:
loadedDocument = new XmlDocument();
loadedDocument.LoadXml ( myXML );
On this loadedDocument I call loadedDocument.ChildNodes, search through them as I whish and get the needed content of a node via node.InnerText. I am not sure why, but for the deeper nested nodes the reader adds leading/trailing whitespaces. It was hard to see in the console, so I did not notice when outputting the paths. Simply using node.InnerText.Trim() solved the matter. Although I wonder why this is happening in the first place.