Question

I am wondering is ther similar solution for saving all kind of data to file like it's done in iOS with plist files?

Loading data to NSArray

[NSMutableArray arrayWithArray:[[NSArray alloc] initWithContentsOfFile:pathToFile]]

Write data to file

[NSArray writeToFile:destPath atomically:YES];

I know there is something similar to [NSUserDefaults standardUserDefaults] and it is IsolatedStorageSettings.ApplicationSettings

Any help will appreciated.


SOLVED

In this question I have exampled use of 1 Soonts method.

Was it helpful?

Solution

There're many solutions.

  1. [built-in] Data contract serializer, to either text/xml or MS Binary XML (the latter being much more effective)
  2. [built-in] XML serializer.
  3. [built-in] BinaryWriter/BinaryReader.
  4. [either built-in or 3-rd party] JSON serializer.
  5. [3-rd party] Google protocol buffers.

For different requirements, I prefer 1 and 5.

Update: here's the sample code implementing #1. The code is tested, but there's still some room for improvements.

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;

namespace NS
{
    /// <summary>This static class handles data serialization for the app.</summary>
    /// <remarks>
    /// <para>The serialization format is Microsoft's .NET binary XML, documented in [MC-NBFX] specification.</para>
    /// <para>The efficiency could be improved further, by providing a pre-built XML dictionary.</para>
    /// </remarks>
    internal static class Serializer
    {
        /// <summary>Serializers are cached here</summary>
        static readonly Dictionary<Type, DataContractSerializer> s_serializers = new Dictionary<Type, DataContractSerializer>();

        /// <summary>Either get the serializer from cache, or create a new one for the type</summary>
        /// <param name="tp"></param>
        /// <returns></returns>
        private static DataContractSerializer getSerializer( Type tp )
        {
            DataContractSerializer res = null;
            if( s_serializers.TryGetValue( tp, out res ) )
                return res;

            lock( s_serializers )
            {
                if( s_serializers.TryGetValue( tp, out res ) )
                    return res;
                res = new DataContractSerializer( tp );
                s_serializers.Add( tp, res );
                return res;
            }
        }

        /// <summary>Read deserialized object from the stream.</summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="stm"></param>
        /// <returns></returns>
        public static T readObject<T>( Stream stm ) where T: class
        {
            DataContractSerializer ser = getSerializer( typeof( T ) );
            using( var br = XmlDictionaryReader.CreateBinaryReader( stm, XmlDictionaryReaderQuotas.Max ) )
                return (T)ser.ReadObject( br );
        }

        /// <summary>Write serialized object to the stream.</summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="stm"></param>
        /// <param name="obj"></param>
        public static void writeObject<T>( Stream stm, T obj ) where T: class
        {
            DataContractSerializer ser = getSerializer( typeof( T ) );
            using( XmlDictionaryWriter bw = XmlDictionaryWriter.CreateBinaryWriter( stm, null, null, false ) )
            {
                ser.WriteObject( bw, obj );
                bw.Flush();
            }
            stm.Flush();
        }
    }
}

OTHER TIPS

I've written an article about various ways of saving data in Windows Phone applications that might be helpful. I've used JSON with an Isolated Storage File in a number of my apps, and there's some sample code there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top