我正在开发一个 WP7 应用程序。该应用程序将有几个 XML 文件。有些是读写的,一些是只读的。我在这里有什么选择?隔离存储?作为资源嵌入?
我也需要知道...
- 我如何将 XML 加载到 XElements 中?
- 我将如何从 XElement 保存到其中一个?
我正在开发一个 WP7 应用程序。该应用程序将有几个 XML 文件。有些是读写的,一些是只读的。我在这里有什么选择?隔离存储?作为资源嵌入?
我也需要知道...
对于写访问,您将需要使用 IsolatedStorage。您可以检查文件是否存在并从 IsolatedStorage 加载它,否则从资源加载它。对于只读访问,您可以从资源中加载它。将 xml 文件添加到您的项目中,检查 Build Action is Content。
XDocument doc = LoadFromIsolatedStorage(name);
if (doc == null)
{
doc = LoadFromResource(name);
}
////////////////////////////
public static XDocument LoadFromResource(string name)
{
var streamInfo = Application.GetResourceStream(new Uri(name, UriKind.Relative));
using(var s = streamInfo.Stream)
return XDocument.Load(s);
}
public static XDocument LoadFromIsolatedStorage(string name)
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists(name))
{
using(var stream = store.OpenFile(name,FileMode.Open))
return XDocument.Load(stream);
}
else
return null;
}
}
public static void SaveToIsolatedStorage(XDocument doc, string name)
{
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
var dir = System.IO.Path.GetDirectoryName(name);
if (!store.DirectoryExists(dir))
store.CreateDirectory(dir);
using (var file = store.OpenFile(name, FileMode.OpenOrCreate))
doc.Save(file);
}
}
只是想一想,您想将“xap”作为包含所有文档的“exe”运行吗?如果是这样,您可以使用工具 Desklighter ( http://blendables.com/labs/desklighter/ )。