Question

I use an observable collection to save a list in my app, and use XML to save that list to isolated storage. the app is ok when i add item, but when i delete an item and restart app, it cant read the XML. I use i tools to get that XML from isolated storage and yes, its structure is wrong.

here is the code

public void saveToXML()
        {
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();                       
            XmlWriterSettings xmlWriterSetting = new XmlWriterSettings();
            xmlWriterSetting.Indent = true;
            using (storage)
            {
                using (IsolatedStorageFileStream fsIdea = new IsolatedStorageFileStream("Idea.xml", FileMode.OpenOrCreate, storage))
                {
                    using (XmlWriter xWIdea = XmlWriter.Create(fsIdea, xmlWriterSetting))
                    {
                        XmlSerializer xSIdea = new XmlSerializer(typeof(ObservableCollection<IdeaViewModel>));
                        xSIdea.Serialize(xWIdea, App.ViewModel.ItemsIdea);
                    }
                }                
            }
        }

        public void readFromXML()
        {
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!storage.FileExists("Idea.xml") )
            {
                saveToXML();
            }
            using (storage)
            {
                using (IsolatedStorageFileStream fsIdea = new IsolatedStorageFileStream("Idea.xml", FileMode.Open, storage))
                {
                    XmlSerializer xSIdea = new XmlSerializer(typeof(ObservableCollection<IdeaViewModel>));
                    App.ViewModel.ItemsIdea = xSIdea.Deserialize(fsIdea) as ObservableCollection<IdeaViewModel>;
                }                
            }
        }

and here is the wrong XML file when i deleted item and restarted app, the error is at line 9, looks like the XML file still keep a part of the deleted item ?

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfIdeaViewModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <IdeaViewModel>
    <IdeaContent>sdfsd</IdeaContent>
  </IdeaViewModel>
  <IdeaViewModel>
    <IdeaContent>sdfds</IdeaContent>
  </IdeaViewModel>
</ArrayOfIdeaViewModel>aContent>sf</IdeaContent>
  </IdeaViewModel>
  <IdeaViewModel>
    <IdeaContent>sdfsd</IdeaContent>
  </IdeaViewModel>
  <IdeaViewModel>
    <IdeaContent>sdfds</IdeaContent>
  </IdeaViewModel>
</ArrayOfIdeaViewModel>

( i put the readFromXML() at constructor of app, the the saveToXML() at here

// 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)
    {
        saveToXML();                     
    }

    // Code to execute when the application is closing (eg, user hit Back)
    // This code will not execute when the application is deactivated
    private void Application_Closing(object sender, ClosingEventArgs e)
    {
        // Ensure that required application state is persisted here.
        saveToXML();            
    }
Was it helpful?

Solution

When you save use FileMode.Create instead of FileMode.OpenOrCreate:

using (IsolatedStorageFileStream fsIdea = new IsolatedStorageFileStream("Idea.xml", FileMode.Create, storage))

In your case you are just editting the old version of the file and thus you probably have some artifacts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top