文件类似于 iOS 中的 Windows Phone 8 中的 plist



我想知道是否有类似的解决方案可以将所有类型的数据保存到文件中,就像在iOS中使用plist文件一样?

将数据加载到NSArray

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

将数据写入文件

[NSArray writeToFile:destPath atomically:YES];

我知道有类似的东西[NSUserDefaults standardUserDefaults]IsolatedStorageSettings.ApplicationSettings

任何帮助将不胜感激。


解决

在这个问题中,我举例说明了 1 个 Soonts 方法的使用。

有很多解决方案。

  1. [内置] 数据协定序列化程序,文本/XML 或 MS 二进制 XML(后者更有效)
  2. [内置] XML 序列化程序。
  3. [内置] BinaryWriter/BinaryReader。
  4. [内置或第三方] JSON 序列化程序。
  5. [第三方] 谷歌协议缓冲区。

对于不同的要求,我更喜欢 1 和 5。

更新:这是实现 #1 的示例代码。代码已经过测试,但仍有一些改进的余地。

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();
        }
    }
}

我写了一篇关于在Windows Phone应用程序中保存数据的各种方法的文章,可能会有所帮助。我已经在我的许多应用中将 JSON 与独立存储文件一起使用,并且那里有一些示例代码。

最新更新