0

I've been trying to save an xml file to the phone for later use. I have tried to save it to Environment.SpecialFolder.MyDocuments, Environment.SpecialFolder.Personal, and Environment.SpecialFolder.LocalApplicationData. All of these work in the app session, as in I can save a file and read from it as long as the app is running. As soon as I close the app and reopen it, the app is supposed to read from the file and display information, but for some reason the file either does not exist (in which case the app creates a new blank one), or it's blank. I want to try and see what the file looks like itself, but I can't even find where it's stored because I just use the Environment.SpecialFolder... path. I've so far only tested this app on android if that helps.

Any help is greatly appreciated!

Here is the code to save data to the file checkins.xml:

//make new check in
//load the document
XmlDocument checkInDoc = new XmlDocument();
checkInDoc.Load(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "checkIns.xml");

//create the nodes
XmlNode checkInNode = checkInDoc.CreateElement("CheckIn");
XmlNode nameNode = checkInDoc.CreateElement("Name");
XmlNode timeNode = checkInDoc.CreateElement("Time");

//assign values to the nodes
nameNode.InnerText = "New Check In";
timeNode.InnerText = "9:00 PM";

//place nodes in document and save
checkInNode.AppendChild(nameNode);
checkInNode.AppendChild(timeNode);
checkInDoc.DocumentElement.AppendChild(checkInNode);
checkInDoc.Save(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "checkIns.xml");

And here is the code used to load the checkins from that file to a list:

List<CheckIn> tempList = new List<CheckIn>();

//Load Checkins from file into checkInList
XmlDocument checkInDoc = new XmlDocument();
checkInDoc.Load(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "checkIns.xml");

foreach(XmlNode node in checkInDoc.SelectNodes("CheckIns/CheckIn"))
{
    tempList.Add(new CheckIn() { Name = node.SelectSingleNode("Name").InnerText, Time = node.SelectSingleNode("Time").InnerText });
}
Paul Kertscher
  • 8,484
  • 5
  • 30
  • 51
Joe Fioti
  • 313
  • 3
  • 13
  • Possible duplicate of [How can I save some user data locally on my Xamarin Forms app?](https://stackoverflow.com/questions/31655327/how-can-i-save-some-user-data-locally-on-my-xamarin-forms-app) – Patrick Roberts Jul 14 '18 at 02:21
  • [This NuGet package](https://github.com/dsplaisted/PCLStorage), suggested in the duplicate target I've cited, has options for persistent local file storage. – Patrick Roberts Jul 14 '18 at 02:22
  • This sounds like you're re-writing the file each run of the app instead of testing if it exists first then doing it. Can you post all the code you're using to make / use the file in all locations? Seeing that will better help you answer the question. It may be a programming error or potentially the wrong file folder. – Michael Puckett II Jul 14 '18 at 02:32
  • It'd be helpful to see your code (a [mcve] if at any rate possible). – Paul Kertscher Jul 16 '18 at 04:53
  • Please see my edit where I added code samples @PaulKertscher – Joe Fioti Jul 16 '18 at 15:01
  • Just a side note: You should prefer `Path.Combine` over manual concatenation of file path parts. Bust this is not the issue of your code. – Paul Kertscher Jul 17 '18 at 04:33
  • @JosephFioti Could you add the code where you are deciding whether to load or create a file? – Paul Kertscher Jul 17 '18 at 04:37
  • Thanks to everyone trying to help, I've decided to go with Application.Current.Preferences to save my data(see my answer) but the help is greatly appreciated! – Joe Fioti Jul 18 '18 at 12:17

1 Answers1

0

I've actually solved this an entirely different way. I couldn't get saving/loading the file to work so I was able to use Application.Current.Properties which holds basic key/value pairs and is persistent between uses. I would highly recommend using this as it has worked perfectly for me.

Joe Fioti
  • 313
  • 3
  • 13