title: How to serialize and deserliaze a Dictionary<string,object> in WinRT (Metro app in C# and XAML)?
body: <p>I've been trying to solve this silly problem for a few days now and it is doing my head in. I would be very happy is somebody has a working example, because the ones I have found so far did not work :( 
  I can serialize basic types, but not objects. And I am very confused in regards to the DataContractAttribute etc. The error I get is:</p>
  
  <blockquote>
    <p>{"Type 'SerializeListWinRT.DataModel.LocalStorage+Cat' with data
    contract name
    'LocalStorage.Cat:http://schemas.datacontract.org/2004/07/SerializeListWinRT.DataModel'
    is not expected. Consider using a DataContractResolver or add any
    types not known statically to the list of known types - for example,
    by using the KnownTypeAttribute attribute or by adding them to the
    list of known types passed to DataContractSerializer."}</p>
  </blockquote>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Windows.Storage;
  using System.IO;
  using System.Runtime.Serialization;
  using Windows.Storage.Streams;
  
  
  namespace SerializeListWinRT.DataModel
  {
      class LocalStorage
      {
          [DataContractAttribute]
          public class Cat
          {
              [DataMember()]
              public String Name { get; set; }
          }
  
          static private Dictionary&lt;string, object&gt; _data = new Dictionary&lt;string, object&gt;();
          private const string filename = "items.xml";
  
          static public Dictionary&lt;string, object&gt; Data
          {
              get { return _data; }
  
          }
  
          static public T GetItem&lt;T&gt;(string key)
          {
              T result = default(T);
  
              if (_data.ContainsKey(key))
              {
                  result = (T)_data[key];
              }
  
              return result;
          }
  
          static public bool ContainsItem(string key)
          {
              return _data.ContainsKey(key);
          }
  
          static async public Task Save()
          {
              await Windows.System.Threading.ThreadPool.RunAsync((sender) =&gt;
              {
                  LocalStorage.SaveAsync().Wait();
              }, Windows.System.Threading.WorkItemPriority.Normal);
          }
  
          static async public Task Restore()
          {
              await Windows.System.Threading.ThreadPool.RunAsync((sender) =&gt;
              {
                  LocalStorage.RestoreAsync().Wait();
              }, Windows.System.Threading.WorkItemPriority.Normal);
          }
  
          static async private Task SaveAsync()
          {
              _data.Add("cat", new Cat { Name = "Myname is" });
              _data.Add("dog", new Cat { Name = "Myname is" });
  
              StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
              IRandomAccessStream sessionRandomAccess = await sessionFile.OpenAsync(FileAccessMode.ReadWrite);
              IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
              DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary&lt;string, object&gt;));
              sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), _data);
              await sessionOutputStream.FlushAsync();
          }
  
          static async private Task RestoreAsync()
          {
              StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
              if (sessionFile == null)
              {
                  return;
              }
              IInputStream sessionInputStream = await sessionFile.OpenReadAsync();
              DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary&lt;string, object&gt;));
              _data = (Dictionary&lt;string, object&gt;)sessionSerializer.ReadObject(sessionInputStream.AsStreamForRead());
          }
      }
  }
  </code></pre>
  
  <p>How I solved the problem:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Windows.Storage;
  using System.IO;
  using System.Runtime.Serialization;
  using Windows.Storage.Streams;
  
  
  namespace SerializeListWinRT.DataModel
  {
      class LocalStorage
      {   
          [KnownType(typeof(SerializeListWinRT.Cat))]
          [DataContractAttribute]
          public class Cat
          {
              [DataMember()]
              public String Name { get; set; }
          }
  
  
      static private Dictionary&lt;string, object&gt; _data = new Dictionary&lt;string, object&gt;();
      private const string filename = "ngt.xml";
  
      static public Dictionary&lt;string, object&gt; Data
      {
          get { return _data; }
  
      }
  
      static public T GetItem&lt;T&gt;(string key)
      {
          T result = default(T);
  
          if (_data.ContainsKey(key))
          {
              result = (T)_data[key];
          }
  
          return result;
      }
  
      static public bool ContainsItem(string key)
      {
          return _data.ContainsKey(key);
      }
  
      static async public Task Save&lt;T&gt;()
      {
          await Windows.System.Threading.ThreadPool.RunAsync((sender) =&gt;
          {
              LocalStorage.SaveAsync&lt;T&gt;().Wait();
          }, Windows.System.Threading.WorkItemPriority.Normal);
      }
  
      static async public Task Restore&lt;T&gt;()
      {
          await Windows.System.Threading.ThreadPool.RunAsync((sender) =&gt;
          {
              LocalStorage.RestoreAsync&lt;T&gt;().Wait();
          }, Windows.System.Threading.WorkItemPriority.Normal);
      }
  
      static async private Task SaveAsync&lt;T&gt;()
      {
          _data.Add("cat", new Cat { Name = "Myname is" });
          _data.Add("dog", new Cat { Name = "Myname is" });
  
          StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
          IRandomAccessStream sessionRandomAccess = await sessionFile.OpenAsync(FileAccessMode.ReadWrite);
          IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
          DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary&lt;string, object&gt;), new Type[] { typeof(T) });
          sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), _data);
          await sessionOutputStream.FlushAsync();
      }
  
      static async private Task RestoreAsync&lt;T&gt;()
      {
          StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
          if (sessionFile == null)
          {
              return;
          }
          IInputStream sessionInputStream = await sessionFile.OpenReadAsync();
          DataContractSerializer sessionSerializer = new DataContractSerializer(typeof(Dictionary&lt;string, object&gt;), new Type[] { typeof(T) });
          _data = (Dictionary&lt;string, object&gt;)sessionSerializer.ReadObject(sessionInputStream.AsStreamForRead());
      }
  }
  </code></pre>
  
  <p>}</p>