XML on iPhone

Hi Everyone,

I am having trouble loading from XML on the iPhone. Here is the code:

function LoadXML()
{
var r : StreamReader = File.OpenText(docPath+“/highscores.xml”);
var _info : String = r.ReadToEnd();
r.Close();
line=_info;
highscores = line.Split(“%”[0]);
Debug.Log(“File Read”);
}

Here is the error I am getting:

DirectoryNotFoundException: Could not find a part of the path “/var/mobile/Applications/F0571EFE-3482-4183-8A8B-4F3498FB14FD/terraslam.app/highscores.xml”.

I am placing the xml file straight in the assets folder. Where should I be putting it? or is the code wrong?

For reference this is also the code I am going to use to write to the XML. I am assuming this is wrong as well if the first bit is.

function CreateXML()
{
var writer : StreamWriter;
var t : FileInfo = new FileInfo(docPath+“/highscores.xml”);
if(!t.Exists)
{
writer = t.CreateText();
}
else
{
t.Delete();
writer = t.CreateText();
}
writer.Write(lineToWrite);
writer.Close();
scoreText.text = “File written.”;
}

Any help appreciated!!!

Thanks

Chris

The code itself should work.

However Unity’s file structure as well as iOS read/write rules could be an issue.

Everything created in Unity gets packaged into a single file (even if it’s in Resources folder) This means that the actual file no longer exists on the device it’s a part of the compressed file Assets_0 or something or other.

You can place it in Streaming Assets like you would a video:
file:///Applications/Unity/Unity.app/Contents/Documentation/Documentation/ScriptReference/iPhoneUtils.PlayMovie.html

This will allow reading, but you may have to fight with it a little bit to get the actual file path in relation to the App.

The second issue you will run into is that the .app folder is not writeable for you. You need to access the Documents folder, as that is your place for creating new files, and writing them to disk.

I use: Application.dataPath + “/…/…/Documents/”;

Provided you don’t have to read a prebuilt XML it could be a good idea to always write to Documents so you don’t have two different locations for a single type of file. I’m not sure of a way to store something in Documents on install to the device (not sure it’s possible).

It backpedals two steps from the data folder and enters Documents.