title: How to serialize Windows.Storage.StorageFile in metro app
body: <p>I'm developing a music metro style app. I'm getting all music files from users <code>music library</code> I want to store <code>StorageFile</code> object, because i don't want to retrieve again and again.To do this i tried serialize <code>StorageFile</code> object and store it into <code>XML</code>. From the examples <a href="https://stackoverflow.com/questions/11118910/how-to-serialize-and-deserliaze-a-dictionarystring-object-in-winrt-metro-app">here</a> and <a href="http://www.irisclasson.com/2012/07/09/example-metro-app-winrt-serializing-and-deseralizing-objects-to-storagefile-and-localfolder-using-generics-and-asyncawait-threading/" rel="nofollow noreferrer">here</a>  i tried to generate <code>XML</code> file, but it throws an exception on creating XML file saying</p>
  
  <blockquote>
    <p>Type 'Windows.Storage.StorageFile' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.</p>
  </blockquote>
  
  <p>So far my code is below,</p>
  
  <pre><code>namespace CloudMusic.AppSettings
  {
      [KnownType(typeof(CloudMusic.AppSettings.MusicFileDict))]
      [DataContractAttribute]
      public class MusicFileDict
      {
          [DataMember]
          public object musicStorageFile { get; set; }
  
          [DataMember]
          public int id { get; set; }
      }
  }
  </code></pre>
  
  <p>and from below class I'm generating <code>XML</code></p>
  
  <pre><code>class GenerateMusicDict
  {    
          private const string filename = "musiclist.xml";
          static private List&lt;MusicFileDict&gt; _data = new List&lt;MusicFileDict&gt;();
  
          static public List&lt;MusicFileDict&gt; Data
          {
              get { return _data; }
          }
          static async public Task Save&lt;T&gt;()
          {
              await Windows.System.Threading.ThreadPool.RunAsync((sender) =&gt; GenerateMusicDict.SaveAsync&lt;T&gt;().Wait(), Windows.System.Threading.WorkItemPriority.Normal);
          }        
  
          internal static async Task SaveAsync&lt;T&gt;()
          {           
              GenerateMusicDict.GenerateMusicFileDict();           
              try
              {
                  StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
                  IRandomAccessStream sessionRandomAccess = await sessionFile.OpenAsync(FileAccessMode.ReadWrite);
                  IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
  
                  var sessionSerializer = new DataContractSerializer(typeof(List&lt;MusicFileDict&gt;), new Type[] { typeof(T) });
                  sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), _data);
                  await sessionOutputStream.FlushAsync();
              }
              catch (Exception)
              {
              }
  
          }
          /// &lt;summary&gt;
          /// Generate music file dictonary according to songs ID
          /// &lt;/summary&gt;
          /// &lt;param name="musicFileList"&gt;&lt;/param&gt;
          private static void GenerateMusicFileDict()
          {
              List&lt;StorageFile&gt; music_file_list = MusicFileDict.GetMusicList(); // At this stage i'll get all my music files,GetMusicList() function returns List&lt;StorageFile&gt;
              for (int i = 0; i &lt; music_file_list.Count; i++)
              {
                  _data.Add(new MusicFileDict { id = i, musicStorageFile = music_file_list[i]});          
              } 
          }
  }
  </code></pre>
  
  <p>and calling Save method from my main class using</p>
  
  <pre><code> await GenerateMusicDict.Save&lt;MusicFileDict&gt;(); 
  </code></pre>
  
  <p>On SaveAsync() function at this line</p>
  
  <pre><code> sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), _data);
  </code></pre>
  
  <p>it's firing an <code>Exception</code> saying</p>
  
  <blockquote>
    <p>Type 'Windows.Storage.StorageFile' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.
    so how can i serialize it? or is there any way that i can store it and re-use. Please help me</p>
  </blockquote>