file storage - Store class instance - Windows store application -


i'm bit new programing windows store app.so question how can save instance of class in xml or binary file.i tried code isn't working.

hope 1 can steer me in right direction .

you can serialize instance using code

    /// <summary>     /// deserializes xml.     /// </summary>     /// <typeparam name="t"></typeparam>     /// <param name="xml">the xml.</param>     /// <returns>the instance</returns>     public static t deserializexml<t>(this string xml)     {         var bytes = encoding.utf8.getbytes(xml);         using (var stream = new memorystream(bytes))         {             var serializer = new datacontractserializer(typeof(t));             return (t)serializer.readobject(stream);         }     }      /// <summary>     /// serializes specified instance.     /// </summary>     /// <param name="instance">the instance.</param>     /// <returns>xml</returns>     public static string serializexml(this object instance)     {         using (var stream = new memorystream())         {             var serializer = new datacontractserializer(instance.gettype());             serializer.writeobject(stream, instance);             stream.position = 0;             using (var reader = new streamreader(stream))             {                 var result = "<?xml version='1.0' encoding='utf-8' ?>";                 result += reader.readtoend();                 return result;             }         }     } 

next step save serialized instance text file.

   var filename = "instance.txt";        var file = await applicationdata.current.localfolder.createfileasync(filename, creationcollisionoption.openifexists);    var content = yourinstance.serializexml();    await fileio.writetextasync(file, content, windows.storage.streams.unicodeencoding.utf8); 

now there should file in apppackage-local-folder called instance.txt contains current instance serialized xml.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -