- Home /
IsolatedStorageException when creating new XmlDocument variable only on android build.
When I run my build on android, I receive an IsolatedStorageException when I try to call
XmlDocument doc = new XmlDocument ();
The exception says "Could not find part of the path", but I am not passing a path at all, and since I'm not loading or saving the file, I don't see why a path would be needed anyway. The path is pointing to /data/app/[game's identifier]/base.apk/SaveDataTemplate.xml
Here's the full script, as well as a screencap of my Device's screen which has a shitty little debugger containing the last error in the file modifying process, since my phone's cable isn't playing nice with ADB. The bug apparently is coming from line 102 of LevelDataCollection.cs:
//File: LevelDataCollection.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System.Xml;
using System.Xml.Serialization;
using System.IO;
[XmlRoot ("LevelCollection")]
public class LevelDataCollection {
[XmlArray ("Levels")]
[XmlArrayItem ("Level")]
public List<LevelData> Levels = new List<LevelData> ();
//If it doesn't work, try adding System.IO. to the beginning of Path.Combine
public static string dataPath = "Levels";
public static string saveDataTemplatePath = Path.Combine (Application.dataPath, "SaveDataTemplate.xml");
public static LevelDataCollection Load (string path) {
StringReader reader = null;
XmlSerializer Serializer = null;
try {
TextAsset LevelContainerXML = (TextAsset)Resources.Load (path);
XmlDocument xmldoc = new XmlDocument ();
xmldoc.LoadXml (LevelContainerXML.text);
Serializer = new XmlSerializer (typeof (LevelDataCollection));
reader = new StringReader (LevelContainerXML.text);
} catch (System.Exception e) {
Debug.LogError (e + "\nCould not find file at " + Path.GetFullPath (path));
return null;
}
LevelDataCollection levelCollection = Serializer.Deserialize (reader) as LevelDataCollection;
reader.Close ();
UpdateSaveDataTemplate (saveDataTemplatePath, levelCollection.Levels);
GameObject.Find ("DebugLine").GetComponent<UnityEngine.UI.Text> ().text = "Loading level data from " + Path.GetFullPath (path);
return levelCollection;
}
public override string ToString () {
string compilation = "";
foreach (LevelData data in Levels){
compilation += "\n";
compilation += data;
}
return compilation;
}
//Since there might not be a save file, we need to create an xml file that will be used as a blank template
public static void UpdateSaveDataTemplate (string templatePath, List <LevelData> dataToClone) {
LevelSaveDataCollection collection = new LevelSaveDataCollection ();
collection.Levels = new List<LevelSaveData> ();
int i = 0;
while (collection.Levels.Count < dataToClone.Count) {
collection.Levels.Add (new LevelSaveData (dataToClone[i].ID.ToString() + ":" + dataToClone[i].SubID.ToString(), i == 0, false));
i++;
}
XmlSerializer Serializer = new XmlSerializer (typeof (LevelSaveDataCollection));
if (!File.Exists (templatePath))
CreateNewStandardXML (templatePath);
FileStream file = new FileStream (templatePath , FileMode.Truncate);
Serializer.Serialize (file, collection);
file.Close ();
Debug.Log ("Template save data XML successfully created at " + file.Name + "! VVV\n\n" + collection);
}
public static void CreateNewStandardXML (string path) {
// Create the XmlDocument.
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("<LevelSaveDataCollection></LevelSaveDataCollection>"); //XML document content (empty, as we just want a basic file);
// Save the document to a file and auto-indent the output.
XmlTextWriter writer = new XmlTextWriter (path, null);
writer.Formatting = Formatting.Indented;
doc.Save (writer);
writer.Close ();
}
}
Screencap Here (Image button wasn't working so I had to use a link instead :P )
Comment