C# Windows Phone 8的iOS中类似于plist的文件
我想知道是否有类似的解决方案可以像在iOS中使用plist文件那样将所有类型的数据保存到文件中 将数据加载到C# Windows Phone 8的iOS中类似于plist的文件,c#,.net,windows-phone,isolatedstoragefile,C#,.net,Windows Phone,Isolatedstoragefile,我想知道是否有类似的解决方案可以像在iOS中使用plist文件那样将所有类型的数据保存到文件中 将数据加载到NSArray [NSMUTABLEARRY arrayWithArray:[[NSArray alloc]initwithcontentsofile:pathToFile]] 将数据写入文件 [NSArray writeToFile:destPath原子化:是] 我知道有类似于[NSUserDefaults standardUserDefaults]的东西,它是隔离存储设置。应用程序设置
NSArray
[NSMUTABLEARRY arrayWithArray:[[NSArray alloc]initwithcontentsofile:pathToFile]]
将数据写入文件
[NSArray writeToFile:destPath原子化:是]代码>
我知道有类似于[NSUserDefaults standardUserDefaults]
的东西,它是隔离存储设置。应用程序设置
任何帮助都将不胜感激
解决了的
我举了一个SOOTS方法的例子。有很多解决方案
[内置]数据协定序列化程序,文本/xml或MS二进制xml(后者更有效)
[内置]XML序列化程序
[内置]BinaryWriter/BinaryReader
[内置或第三方]JSON序列化程序
[3-第三方]谷歌协议缓冲区
对于不同的要求,我更喜欢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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Runtime.Serialization;
使用System.Xml;
名称空间NS
{
///此静态类处理应用程序的数据序列化。
///
///序列化格式是Microsoft的.NET二进制XML,记录在[MC-NBFX]规范中。
///通过提供预构建的XML字典,可以进一步提高效率。
///
内部静态类序列化程序
{
///序列化程序缓存在这里
静态只读字典s_序列化程序=新建字典();
///从缓存中获取序列化程序,或者为该类型创建一个新的序列化程序
///
///
专用静态DataContractSerializer getSerializer(类型tp)
{
DataContractSerializer res=null;
if(s_serializers.TryGetValue(tp,out res))
返回res;
锁(s_序列化程序)
{
if(s_serializers.TryGetValue(tp,out res))
返回res;
res=新的DataContractSerializer(tp);
添加(tp,res);
返回res;
}
}
///从流中读取反序列化对象。
///
///
///
公共静态T readObject(Stream stm),其中T:class
{
DataContractSerializer ser=getSerializer(typeof(T));
使用(var br=XmlDictionaryReader.CreateBinaryReader(stm,XmlDictionaryReaderQuotas.Max))
返回(T)序列读取对象(br);
}
///将序列化对象写入流。
///
///
///
公共静态void writeObject(Stream stm,T obj),其中T:class
{
DataContractSerializer ser=getSerializer(typeof(T));
使用(XmlDictionaryWriter bw=XmlDictionaryWriter.CreateBinaryWriter(stm,null,null,false))
{
ser.WriteObject(bw,obj);
bw.Flush();
}
stm.Flush();
}
}
}
我写了一篇可能有用的文章。在我的许多应用程序中,我都将JSON与一个独立的存储文件一起使用,其中有一些示例代码。你能分享一个示例吗?我想保存ObservableCollection。感谢您提供这些提示,我还使用Reference更新了我的问题,并使用anwser从基础解释了另一个问题。