views:

559

answers:

3

I need to read, edit and save one xml file. In other words, I want to write again over the same file and not to create a new xml file. Reading and editing parts work, but not the saving.

The Linq-to-Xml code for saving is simply:

doc.Save(this.Path);

This works in iPhone Simulator but throws System.UnauthorizedAccessException on the device. The xml file is decorated as "content" in MonoDevelop.

Any help appreciated.

/pom

A: 

You are only allowed to save into your application's document directory. You can get it like this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

What is the value of this.Path?

Don
Thank you, this is probably what I need. However, I need now to figure out how to call that function in MonoTouch.The value of this.Path is the just name of the xml file. I'm using the same value to load the xml file, and that works.
Pompair
After a bit of googling found reference to System.Environment.SpecialFolder.MyDocuments and trying to se it like this:string docpath = System.Environment.SpecialFolder.MyDocuments.ToString().Replace("Personal","Documents");string mypath = System.IO.Path.Combine(docpath, this.Path);doc.Save(mypath);mypath's value is Documents/myxml.xmlHowever this doesn't work either, it throws "System.IO.IsolatedStorage.IsolatedStorageException: Could not find a part of the path"
Pompair
See this (very complete) answer here: http://stackoverflow.com/questions/1829954/write-to-a-file-in-monotouch
Don
+5  A: 

You need to save the file into one of the locations that you have write permissions on:

The /Document directory can be retrieved using: var documents = Environment.GetFolderPath (Environment.SpecialFolder.Personal);

Our "How To Store Files" page has more details:

http://wiki.monotouch.net/HowTo/Files/HowTo:_Store_Files

miguel.de.icaza
A: 

var documents = Environment.GetFolderPath (Environment.SpecialFolder.Personal); var document = Directory.GetFiles(documents);

Anonymous