在IsolatedStorage中编辑XML的问题

时间:2014-02-22 05:59:57

标签: c# xml windows-phone-8

处理使用独立存储编辑XML的程序(必须始终在隔离存储中)。但是,当我运行我的代码时,我正在接受一个例外:

mscorlib.ni.dll中出现'System.IO.IsolatedStorage.IsolatedStorageException'类型的第一次机会异常

编辑1:还收到:mscorlib.ni.dll中出现“System.Xml.XmlException”类型的异常,并且在托管/本地边界之前未处理(偶尔)

以下是代码:

    var storage = IsolatedStorageFile.GetUserStoreForApplication();
    using (Stream stream = storage.OpenFile("voicecmd.xml", FileMode.Truncate, FileAccess.ReadWrite, FileShare.ReadWrite))
    {
        XDocument str1 = XDocument.Load(stream);
        var ns = XNamespace.Get("http://schemas.microsoft.com/voicecommands/1.0");
        str1.Root.Element(ns + "CommandSet").Element(ns + "CommandPrefix").Value = texb.Text;
        str1.Save(stream, SaveOptions.None);
        await VoiceCommandService.InstallCommandSetsFromFileAsync(new Uri("ms-appdata:///local/voicecmd.xml"));

1 个答案:

答案 0 :(得分:2)

我的猜测是那些例外只是最外层的例外。这对他们来说是内在的原因:

您正在使用FileMode.Truncate打开文件,然后尝试使用XDocument.Load()从流中读取

http://msdn.microsoft.com/en-us/library/system.io.filemode(v=vs.110).aspx
“指定操作系统应该打开现有文件。当文件打开时,应该截断它,使其大小为零字节。这需要FileIOPermissionAccess.Write权限。尝试从FileMode.Truncate打开的文件中读取导致ArgumentException异常。“

另外因为有windows-phone-8标签,并且存在涉及Windows商店的可能性:

http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile(v=vs.110).aspx “隔离存储不适用于Windows应用商店应用。而是使用Windows运行时API中包含的Windows.Storage命名空间中的应用程序数据类来存储本地数据和文件。有关详细信息,请参阅Windows开发人员中心中的应用程序数据。 “