This project is read-only.

Problem with deserialization

Aug 9, 2011 at 1:38 PM

Ok, heres my problem. Im using sharpSerializer to save a class with a multidimensional array to isolated storage. I've got a save and a load method that look like this

 

   public void saveSettings()
        {

            App app = (App)Application.Current;
            //Lokale Settings werden generiert
            Settings set;
            if (app.settings == null)
            {
                set = new Settings();
            }
            else
            {
                
                set = app.settings;
            }

            

            set.setDifficulties(difficulties);

  

            set.setMachines(usesmachines);
            app.settings = set;


            
            SharpSerializer sharpSerializer = new SharpSerializer();

            var memoryStream = new MemoryStream();
            


            sharpSerializer.Serialize(app.settings, memoryStream);

          

            
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
                        
            using (var file = appStorage.OpenFile("class1.xml", FileMode.OpenOrCreate))

            using (var writer = new StreamWriter(file))
            {
                writer.Write(memoryStream);
            }           


        }

 

 

 

        public void loadSettings()
        {
            //XmlSerializer xmlSerializer = new XmlSerializer(typeof(Settings));
            SharpSerializer sharpSerializer = new SharpSerializer();

            var appStorage = IsolatedStorageFile.GetUserStoreForApplication();

            if (appStorage.FileExists("class1.xml"))
            {
              using (var file = appStorage.OpenFile("class1.xml", FileMode.Open, FileAccess.ReadWrite))
             { 
                var streamReader = new StreamReader(file);
                

                 var set = sharpSerializer.Deserialize(file);
                
              }
            }
            else
            {
                saveSettings();             
            }
        }

 

 

the save-Method seams to work fine. However when the load-Methos is used i get an error at the deserialization. At first i got an error with the fileacces. I changed it to FileAccess.ReadWrite. Now my internal exception is this

 

{"Data at the root level is invalid. Line 1, position 1."}

 

 Btw. Using 

sharpSerializer.Serialize(app.settings, "class1.xml");

in the save-Method lead to this expection

 

System.MethodAccessException was unhandled  Message=Attempt to access the method failed: System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess)  StackTrace:       at Polenter.Serialization.SharpSerializer.Serialize(Object data, String filename)       at iGetFit.MainPage.saveSettings()       at iGetFit.MainPage.loadSettings()       at iGetFit.MainPage.MainPage_Loaded(Object sender, RoutedEventArgs e)       at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

 

 

Can anyone help me? im really lost here.

Aug 12, 2011 at 6:40 PM
Edited Aug 12, 2011 at 6:41 PM

why so complicated? I don't know what is the reason for your exception, but this syntax of using SharpSerializer is nowhere to find.

I have reformated your example and tried to use in VS2010 and WP7 emulator. It works flawless.

 

public MainPage()
{
    InitializeComponent();

    // Make your settings class
    var settings1 = new string[] { "Alfa", "Beta", "Gamma" };

    // Serialize it
    saveSettings(settings1);

    // Deserialize it
    var settings2 = loadSettings();

    // Compare settings1 and settings2
    // (...use your own comparing method...)
}


 

public void saveSettings(object settings)
{
    SharpSerializer sharpSerializer = new SharpSerializer();

    using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (var file = appStorage.OpenFile("class1.xml", FileMode.OpenOrCreate))
        {
            sharpSerializer.Serialize(settings, file);
        }               
    }
}



public object loadSettings()
{
    SharpSerializer sharpSerializer = new SharpSerializer();

    var appStorage = IsolatedStorageFile.GetUserStoreForApplication();

    if (appStorage.FileExists("class1.xml"))
    {
        using (var file = appStorage.OpenFile("class1.xml", FileMode.Open, FileAccess.ReadWrite))
        {
            return sharpSerializer.Deserialize(file);
        }
    }
    return null;
}

 

p.s. Streams are very powerful, please read sth about MemoryStream, FileStream and writing/reading to/from streams. It's worth the effort.

and don't give up :-)