Logo
HOW TO
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
 
 
Windows Phone

Windows Phone 7 : Isolated Storage - Saving Serialized Data

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
 
 
REVIEW
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
 
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
 
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
Popular keywords
HOW TO Swimlane in Visio Visio sort key Pen and Touch Creating groups in Windows Server Raid in Windows Server Exchange 2010 maintenance Exchange server mail enabled groups Debugging Tools Collaborating
Top 10
- Microsoft Excel : How to Use the VLookUp Function
- Fix and Tweak Graphics and Video (part 3) : How to Fix : My Screen Is Sluggish - Adjust Hardware Acceleration
- Fix and Tweak Graphics and Video (part 2) : How to Fix : Text on My Screen Is Too Small
- Fix and Tweak Graphics and Video (part 1) : How to Fix : Adjust the Resolution
- Windows Phone 8 Apps : Camera (part 4) - Adjusting Video Settings, Using the Video Light
- Windows Phone 8 Apps : Camera (part 3) - Using the Front Camera, Activating Video Mode
- Windows Phone 8 Apps : Camera (part 2) - Controlling the Camera’s Flash, Changing the Camera’s Behavior with Lenses
- Windows Phone 8 Apps : Camera (part 1) - Adjusting Photo Settings
- MDT's Client Wizard : Package Properties
- MDT's Client Wizard : Driver Properties
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro