#Win8Dev Tutorial: Serializing Objects in your WinRT Applications using XmlDeSerializer



As a Windows 8 Store application developer, we need to store and retrieve data to and from XML file stored in the application directory. There are various ways to store data in XML but this post will cover a simplest way to do that.

 

Here we will use a 3rd party extension in our application as assembly reference and use their XmlDeSerializer class to handle it easily. Keep reading to know more about it.

 

Installation of TCD.Serialization Library

XmlDeSerializer is a class present in the TCD.Serialization assembly which can be use to Serialize and deserialize XML and JSON to/from Stream or string. You can use this assembly in various platforms like Silverlight 4.0 or higher, Windows Phone 7.0 or higher and Windows 8 Store (WinRT).

 

It is very easy to install in your project as the library is present as NuGet Package. Open your Visual Studio project and within Visual Studio IDE, navigate to Tools –> Library Package Manager –> Package Manager Console as demonstrated below:

 

Open the NuGet Package Manager

 

If you didn’t find that option listed in your Visual Studio IDE, that means you don’t have the NuGet extension. You can install it from the Extension Manager of the IDE. A detailed step is available here: “Installing NuGet Extension”.

 

Now once you open the Package Manager Console, type the following command to install the latest version of the TCD.Serialization library:

Installing TCD.Serialization library using the NuGet Package Manager

 

Once it has been installed, you will get a success message in the screen and it will add the assembly reference to the open project automatically.

 

Serializing and DeSerializing Objects using TCD.Serialization Library

You can serialize and/or deserialize objects using the TCD.Serialization.Xml.XmlDeSerializer class. The class has four static methods to do the required operation with the Data and XML file. Here is the meta data of the class:

 
public static void SerializeToStream(Stream streamObject, object objForSerialization);
public static object DeserializeFromStream(Stream streamObject, Type serializedObjectType);
public static string SerializeToString(object objForSerialization);
public static object DeserializeFromString(string serializedObject, Type serializedObjectType);

 

Let’s check out how to integrate it in the project. First create an async method and pass the object as parameter to it. Now get the handle of the Storage Folder where you want to save and retrieve data form and create an XML file using the CreateFileAsync(...) method of the StorageFolder class.

 

 

To know more about Storage Folder and Files, read this chapter: “Handling Files and Folders in your WinRT apps

 

 

To serialize the object as XML file, call the XmlDeSerializer.SerializeToStream(…) method as shown below:

 
public static async void SaveData(ObservableCollection<Employee> data)
{
    var file = await storageFolder.CreateFileAsync(DATA_FILE,
                                            CreationCollisionOption.ReplaceExisting);
    try
    {
        // serial data object to XML file specified in "DATA_FILE"
        XmlDeSerializer.SerializeToStream(await file.OpenStreamForWriteAsync(), data);
    }
    catch (Exception ex)
    {
        // handle any kind of exceptions
    }
}

 

 

Similarly to deserialize the XML content as object, call the XmlDeSerializer.DeserializeFromStream(…) method as shown in the below code snippet:

 
public static async Task<ObservableCollection<Employee>> GetData()
{
    var file = await storageFolder.CreateFileAsync(DATA_FILE,
     CreationCollisionOption.OpenIfExists);
    try
    {
        // deserialize the collection object from "DATA_FILE" specified
        return XmlDeSerializer.DeserializeFromStream(await file.OpenStreamForReadAsync(), 
                                                     typeof(ObservableCollection<Employee>)) 
                                                     as ObservableCollection<Employee>;
    }
    catch (Exception ex)
    {
        // handle any kind of exception
    }
 
    return null;
}

 

 

This way you can serialize and deserialize objects to and from XML file. This is very simple to implement if you installed the 3rd party library TCD.Serialization package.

 

End Note

I hope that, this chapter will be useful to you. If you didn’t read the previous chapter of this Windows 8 Store Tutorial series yet, you can read them from here: “Windows 8 Store Application Development Tutorial”.

 

Connect with me on Twitter, Facebook and Google+. Also don’t forget to subscribe to my blog’s RSS Feed and Email Newsletter to get updates directly delivered to your inbox. Cheers.

1 comments


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top