XML and JSON (De)Serialization in WinRT and Windows Phone

In some of my recent Metro/WinRT projects I had to save data to the disk. I decided to go for Xml-serialzation, because it’s human-readable, and flexible. Also it was perfect for the data I wanted to save (eg. metadata about a movie).

(If you want to do it on your own: System.IO and System.Xml.Serialization are the namespaces you need for XML (de)serialization.)

As I needed this in many projects, I composed a small library, containing a TCD.Serialization.XmlDeSerializer as well as a TCD.Serialization.JsonDeSerializer. Both have four static methods to serialize to/from Stream and string.

You can download a package with the sources and a ready-to-use dll.

TCD.Serialzation is available on NuGet it works with Windows Phone as well.

NOTE: the methods aren’t fail-safe, so you might need to try{}catch{} them to handle exceptions. (For example if you want to serialize to a FileStream without write permission.)

Have fun and let me hear what you think =)

UPDATE: I just migrated the sources to Windows 8 Release Preview 8)

UPDATE: TCD.Serialization is now available on NuGet!! (NuGet instructions)

About these ads

4 thoughts on “XML and JSON (De)Serialization in WinRT and Windows Phone

  1. I’m happy to help :)
    You mean the System.Xml namespace? XML is very powerful, so you should be specific with what you want to do. In general the XML-related methods available in WinRT do not differ from regular Desktop environments too much.

  2. Getting error

    Install-Package : Could not install package ‘TCD.Serialization 1.1.0′. You are trying to install this package into a project that targets ‘.NETCore,Version=v4.5′, but the package
    does not contain any assembly references that are compatible with that framework. For more information, contact the package author.
    At line:1 char:1
    + Install-Package TCD.Serialization
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Install-Package], InvalidOperationException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand

    • I cannot reproduce this problem. Check if your NuGet Package Manager is the latest version (<2.1, or <2.0 does not support portable class libraries..)
      cheers

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s