C#

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)

4 comments

  1. Jorge Garmilla Mancho

    Hey !! … your code was very useful for me .. thanks. Do you know any tutorial (with samples) about the XML Namespace in WinRT ???. I am not finding nothing. :-/

  2. thecake

    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.

  3. Farhan

    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

    1. thecake

      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

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.