Deserializing XML not working with WebGL
Hallo everyone,
I am trying to create a working WebGL version of my application. To do so I need to deserialize an XML file called hspositions.xml, located in the StreamingAssets directory. This works fine in the Editor and Standalone, but not in WebGL. Right now I’m only testing locally with Firefox.
I already asked the question on the forum, but no solution came up so far.
What is left are two error messages showing in the Firefox Browser Console, that I can't get rid of:
Error: WebGL: texImage2D: invalid typed array type for given texture data type WebGL.js:1
and
"IsolatedStorageException: Could not find a part of the path "/file:/D:/Unity_Projects/Axion800_Kabine_DebugWebGL/WebGL/StreamingAssets/hspositions.xml". " WebGL.js:1
Here is my DataCollection class which contains the class structure to store the deserialized XML content as well as the DeSerialize() method:
[XmlRoot("Data")]
public class DataCollection
{
[XmlElement("Hotspot")]
public Hotspot[] Hotspot = new Hotspot[24];
public static DataCollection DeSerialize(string path)
{
XmlSerializer serializer = new XmlSerializer(typeof(DataCollection));
using(var stream = new FileStream(path, FileMode.Open))
{
return serializer.Deserialize(stream) as DataCollection;
}
}
}
public class Hotspot
{
[XmlElement("Properties")]
public Properties[] Properties= new Properties[360];
}
public class Properties
{
public float xPos;
public float yPos;
public float alphaValue;
}
And here is my XMLDeserializer class that calls the DeSerialize() method:
public class XMLDeserializer : MonoBehaviour
{
public static DataCollection XmlData;
public static string filePath;
public static string result = "";
IEnumerator Example()
{
if(filePath.Contains("://"))
{
WWW www = new WWW(filePath);
yield return www;
result = www.url;
XmlData = DataCollection.DeSerialize(result);
}
else
{
XmlData = DataCollection.DeSerialize(filePath);
}
}
void Awake ()
{
filePath = Path.Combine(Application.streamingAssetsPath, "hspositions.xml");
StartCoroutine("Example");
}
}
As the documentation suggests, I am using the WWW class to extract the filePath in the case of a URL.
Can anyone give me a hint into the right direction?
Thanks a lot!
Sean
Answer by SeanM3D · Nov 24, 2015 at 12:10 PM
Found a solution for reading the XML file when it's in the StreamAssets folder. That works locally in Firefox. See #9 here:
http://forum.unity3d.com/threads/loading-xml-from-streamassets-not-working.369564/#post-2394650
Before finding that solution I used a workaround with Resources.Load. For details see posting #8 here:
http://forum.unity3d.com/threads/loading-xml-from-streamassets-not-working.369564/#post-2394650