Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Phone

Windows Phone 7 : Isolated Storage - Saving Serialized Data

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
7/4/2012 4:20:24 PM

1. Problem

You want to serialize on disk the state of your objects, to load them later.

2. Solution

You must use XmlSerializer in combination with the IsolatedStorage classes to serialize the state of your objects in a file.

3. How It Works

The XmlSerializer class serializes (and deserializes) objects to and from XML documents. In serialization, we convert an object and its properties to a serial format (in this case, XML) that can be stored (in our case) or transported (in the case of services, for example). Deserialization is the process of re-creating the object from XML, by decorating the objects that correspond to XmlElement, and their properties that correspond to XmlAttribute.

4. The Code

For this recipe, we have chosen to complete the application logic of 7Drum by using the ExerciseManager class to save and load the exercises.

Just to refresh your memory, as you can see from the diagram in Figure 1, one of the entities involved in this recipe is ExerciseSettings, whose persistence logic (in this case, in the isolated storage) is handled by the ExerciseManager class, which deals simply with loading and saving a list of exercises.

Figure 1. Class Diagram of classes that works with exercise in 7Drum

The first method that you want to consider is Save. The code is written as follows:

public static void Save(List<ExerciseSettings> exercises)
{
        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
        using (var fileStream = store.CreateFile("exercises.xml"))
        using (var writer = new StreamWriter(fileStream))

					  

{
                XmlSerializer ser = new XmlSerializer(typeof(List<ExerciseSettings>));
                ser.Serialize(writer, exercises);
                writer.Close();
        }
}

					  

This code, which makes extensive use of the construct using, behaves almost exactly the same way as the code of the previous recipe. In practice, you return the store associated with your application. Then in this store, you create the XML files for the exercises, you associate the StreamWriter to this fileStream, and finally, thanks to the XmlSerializer class, you write the XML serialization that results in the list of exercises. Closing the stream commits the changes, and the using statements coming at the end of the scope disposes the three variablese writer, fileStream and store.

In this way, you just serialized into isolated storage a list of ExerciseSettings. It is obvious that if your application requires more than one entity domain, you will have no problem using this code. If you have any doubts, the only thing you need to revise is the use of the XmlSerializer class.

The other method to analyze now is the Load method, which will deserialize the data from the XML file in our class:

public static List<ExerciseSettings> Load()
{
  List<ExerciseSettings> exercises = new List<ExerciseSettings>();
  using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
  if (storage.FileExists("exercises.xml"))
  {
    using (IsolatedStorageFileStream stream = storage.OpenFile("exercises.xml",
                FileMode.Open))
    {
      XmlSerializer xml = new XmlSerializer(typeof(List<ExerciseSettings>));
      exercises = xml.Deserialize(stream) as List<ExerciseSettings>;
      stream.Close();
    }
  }
  return exercises;
}

					  

This code is not so difficult to understand if you analyze it step by step:

  1. Create a list of ExerciseSettings.

  2. Return to our store (at this point, it's clear that if you want to access the isolated storage this is a necessary step).

  3. If there is an XML file called exercises in the store, do the following:

    1. Open a stream pointing to the file.

    2. Create an XmlSerializer class to list the ExerciseSettings.

    3. Deserialize the stream in the list of ExerciseSettings.

    4. Close the stream.

At this point, our application 7Drum is ready to save and upload lists of exercises, to keep them between sessions of our application.

5. Usage

After adding the implementation of the two methods of the class ExerciseManager, start the application from Visual Studio by pressing F5. To test if you have correctly written the code save a list of exercises and close 7Drum. Then reopen and check that between sessions the whole list of exercises has been saved.

Other -----------------
- Windows Phone 7 : Saving a File in Isolated Storage and Loading It
- Windows Phone 7 : Media Management - Adding Integration with the Music-Videos Hub
- Windows Phone 7 : Sounding Out with Game Audio - Playing Music
- Windows Phone 7 : Playing Sound Effects
- Microsoft XNA Game Studio 3.0 : Creating Game Components - Adding Artificial Intelligence
- Microsoft XNA Game Studio 3.0 : Creating Game Components - Adding 100 Killer Tangerines
- Windows Phone 7 : Using the Microphone in the Funny Repeater Application
- Windows Phone 7 Advanced UI Development : The Microsoft Advertising SDK
- Microsoft XNA Game Studio 3.0 : Creating Game Components - Constructing Class Instances
- Microsoft XNA Game Studio 3.0 : Creating Game Components - Objects and Abstraction
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server