- Home /
Save data through xml Serialization on iOS
Hey
I ran into a problem today testing my project on my iPhone the first time which seems to be connected to the xml Serialization (i think), as xCode tells me: - Completed reload, in 0.183 seconds -> applicationDidBecomeActive() IsolatedStorageException: Could not find a part of the path "/var/mobile/Applications/FFC4693A-C632-4DE3-8324-4451DD39C1BC/FahrSimulator.app/Data/Resources/data.xml". at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in :0 at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in :0 at System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) [0x00000] in :0 at System.IO.StreamWriter..ctor (System.String path) [0x00000] in :0 at MainMenuScript.Save () [0x00000] in :0 at MainMenuScript.Start () [0x00000] in :0
So I already set my project to .net 2.0 subset. So I read smth about using the resources folder to get the data from there but I ran into a problem.
My code so fare is:
function Save() { var serializer : XmlSerializer = new XmlSerializer(typeof(PlayerScores)); var writer : System.IO.StreamWriter = new System.IO.StreamWriter(Application.dataPath + "/Resources/data.xml"); serializer.Serialize(writer, player); writer.Close(); }
function Load() { var serializer : XmlSerializer = new XmlSerializer(typeof(PlayerScores)); var reader : System.IO.StreamReader = new System.IO.StreamReader(Application.dataPath + "/Resources/data.xml"); player = serializer.Deserialize(reader) as PlayerScores; reader.Close(); }
I read smth about making the file a text file and calling it like this:
var textAsset : TextAsset = Resources.Load("/data", typeof(TextAsset));
But I'm not sure how to continue with that variable.
Could anybody fill in this variable in the script I posted a both? Or is there another problem/solution I have to face?
Best regards,
Fred
Answer by fred_gds · Mar 30, 2013 at 10:14 PM
var textAsset : TextAsset = Resources.Load("/data", typeof(TextAsset));
var serializer : XmlSerializer = new XmlSerializer(typeof(PlayerScores));
var writer : System.IO.TextWriter = new System.IO.StreamWriter(textAsset.text); <--- NullReferenceException
serializer.Serialize(writer, player);
writer.Close();
I tried to implement the textAsset like this but then I get a NullReferenceException. :(