InvalidOperationExecption when trying to save file

nat45928

Senior Member
Feb 1, 2010
75
0
0
Hi,

I am trying to save data when the user click out of my app and i keep getting this erro:

System.InvalidOperationException was unhandled
Message=This operation would create an incorrectly structured document.
StackTrace:

here is the C# code that i am trying to run in my databound app in the App.xmal.cs page:

Code:
// Code to execute when the application is deactivated (sent to background)
        // This code will not execute when the application is closing
        private void Application_Deactivated(object sender, DeactivatedEventArgs e)
        {
            var storage = IsolatedStorageFile.GetUserStoreForApplication();

            if (storage.DirectoryExists("data") && storage.FileExists("data\\data.xml"))
            {
                storage.DeleteFile("data\\data.xml");
                using (IsolatedStorageFileStream storageFile = new IsolatedStorageFileStream("data\\data.xml", FileMode.Create, storage))
                {
                    XDocument xdoc = new XDocument(new XElement("Alarms"));
                    foreach (ItemViewModel alarm in App.ViewModel.Alarms)
                    {
                        xdoc.Add(
                            new XElement("Alarm",
                                new XAttribute("Name", alarm.Name),
                                new XAttribute("Playlist", alarm.Playlist),
                                new XAttribute("Time", alarm.Time.ToShortTimeString()),
                                new XAttribute("TimeString", alarm.TimeString),
                                new XAttribute("Active", alarm.Active.ToString())));
                    }

                    xdoc.Save(storageFile);
                    storageFile.Dispose();
                    storageFile.Close();
                }
            }
        }
Can anyone tell me what is wrong and how to go about fixing it?
Thanks,
Nat
 

SimzzDev

Senior Member
Jan 29, 2011
203
35
0
simzz.com
Hi,

I am trying to save data when the user click out of my app and i keep getting this erro:

System.InvalidOperationException was unhandled
Message=This operation would create an incorrectly structured document.
StackTrace:

here is the C# code that i am trying to run in my databound app in the App.xmal.cs page:

Code:
// Code to execute when the application is deactivated (sent to background)
        // This code will not execute when the application is closing
        private void Application_Deactivated(object sender, DeactivatedEventArgs e)
        {
            var storage = IsolatedStorageFile.GetUserStoreForApplication();

            if (storage.DirectoryExists("data") && storage.FileExists("data\\data.xml"))
            {
                storage.DeleteFile("data\\data.xml");
                using (IsolatedStorageFileStream storageFile = new IsolatedStorageFileStream("data\\data.xml", FileMode.Create, storage))
                {
                    XDocument xdoc = new XDocument(new XElement("Alarms"));
                    foreach (ItemViewModel alarm in App.ViewModel.Alarms)
                    {
                        xdoc.Add(
                            new XElement("Alarm",
                                new XAttribute("Name", alarm.Name),
                                new XAttribute("Playlist", alarm.Playlist),
                                new XAttribute("Time", alarm.Time.ToShortTimeString()),
                                new XAttribute("TimeString", alarm.TimeString),
                                new XAttribute("Active", alarm.Active.ToString())));
                    }

                    xdoc.Save(storageFile);
                    storageFile.Dispose();
                    storageFile.Close();
                }
            }
        }
Can anyone tell me what is wrong and how to go about fixing it?
Thanks,
Nat
Honestly, I don't know what the problem is, but I prefer to use serialization with JSON. It is usually easier. With only one line of code (excluding the stream/saving stuff) you can turn your IEnumerable<Alarm> instance into text.
 
  • Like
Reactions: mikaelel

darthveda

Senior Member
Oct 18, 2009
107
13
0
Bangalore
Code:
XDocument xdoc = new XDocument(new XElement("Alarms"));
                    foreach (ItemViewModel alarm in App.ViewModel.Alarms)
                    {
                        xdoc.Add(
                            new XElement("Alarm",
                                new XAttribute("Name", alarm.Name),
                                new XAttribute("Playlist", alarm.Playlist),
                                new XAttribute("Time", alarm.Time.ToShortTimeString()),
                                new XAttribute("TimeString", alarm.TimeString),
                                new XAttribute("Active", alarm.Active.ToString())));
                    }
you are creating an element "Alarms" at the root of the document and then you are adding new element after that.. rather than in it..
your document would be like this
<Alarms/>
<Alarm Name=alarmname Playlist=playlistname Time=alarmTime TimeString= alarmTimeString Activie=boolActive/>
<Alarm Name=alarmname Playlist=playlistname Time=alarmTime TimeString= alarmTimeString Activie=boolActive/>
<Alarm Name=alarmname Playlist=playlistname Time=alarmTime TimeString= alarmTimeString Activie=boolActive/>


this is not a structured XML document
it should be like this:
<Alarms>
<Alarm Name=alarmname Playlist=playlistname Time=alarmTime TimeString= alarmTimeString Activie=boolActive/>
<Alarm Name=alarmname Playlist=playlistname Time=alarmTime TimeString= alarmTimeString Activie=boolActive/>
<Alarm Name=alarmname Playlist=playlistname Time=alarmTime TimeString= alarmTimeString Activie=boolActive/>
</Alarms>


Try using this code:

Code:
XElement Alarms = new XElement("Alarms", 
                                                        from alarm in App.ViewModel.Alarms
                                                        select new XElement("Alarm", 
                                                                  new XElement("Name", alarm.Name),
                                                                  new XElement("Playlist", alarm.Playlist),
                                                                  new XElement("Time", alarm.Time.ToShortTimeString()),
                                                                  new XElement("TimeString", alarm.TimeString),
                                                                  new XElement("Active", alarm.Active.ToString()));
XDocument xDoc = new XDocument(Alarms);
I prefer using elements rather than attributes, you can replace XElement to XAttribute if you prefer.