如何在 Windows Phone xap 部署中包含 XML 数据?

发布于 2024-09-15 21:12:49 字数 173 浏览 5 评论 0原文

我正在开发 WP7 应用程序。该应用程序将有几个 XML 文件。有些是可读写的,有一些是只读的。我在这里有什么选择?隔离存储?作为资源嵌入?

我还需要知道...

  1. 如何将 XML 加载到 XElements 中?
  2. 我如何从 XElement 保存到其中一个?

I'm working on a WP7 app. The app will have a couple of XML files. Some are read-write, a couple are read-only. What are my options here? IsolatedStorage? Embedding as resources?

I'll also need to know...

  1. How would I load the XML into XElements from either?
  2. How would I save from an XElement into either?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人爱我别走 2024-09-22 21:12:49

对于写访问,您需要使用IsolatedStorage。您可以检查文件是否存在并从isolatedStorage加载它,否则从资源加载它。对于只读访问,您只需从资源加载它即可。将 xml 文件添加到您的项目中,检查“构建操作”是否为“内容”。

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

For write access you will need to use IsolatedStorage. You can check if the file exists and load it from IsolatedStorage otherwise load it from a resource. For read-only access you can just load it from a resource. Add the xml files to your project check 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);
    }
}
青朷 2024-09-22 21:12:49

只是想一想,您想将“xap”作为包含所有文档的“exe”运行吗?如果是这样,您可以使用 Desklighter 工具 (http://blendables.com/labs/desklighter/ )。

Just for thought, do you wanna run the "xap" as an "exe" with all documents included? If so you can use the tool Desklighter (http://blendables.com/labs/desklighter/).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文