将 XML 写入文件时出现 UnauthorizedAccessException [英] UnauthorizedAccessException on writing XML to a file

查看:29
本文介绍了将 XML 写入文件时出现 UnauthorizedAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Windows 8 商店应用程序中使用一些数据绑定.在我的项目中,我有一个名为 Data 的文件夹.其中有一个文件,Names.xml

I am trying to work with some Data Binding in a Windows 8 Store App. In my project I have a Folder which I called Data. In this there's one file, Names.xml

xml 如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<names>
  <name>TestName1</name>
</names>

我可以使用这个方法读取这个文件:

I can read from this file, using this method:

public async void readFromXML()
{
    var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
    _Folder = await _Folder.GetFolderAsync("Data");

    var _File = await _Folder.GetFileAsync("Names.xml");

    XmlDocument document = new XmlDocument();
    var _Content = await Windows.Storage.FileIO.ReadTextAsync(_File);

    document.LoadXml(_Content);

    XmlNodeList names = document.SelectNodes("/names/name");

    foreach(var node in names)
    {
        Insert(0, new Person(node.InnerText));
    }
}

这一切都奏效了,而且这个名字确实会被读取.

This all works and the one name does get read.

现在我想以类似的方式(或任何真正有效的方式...)将新数据保存到这个 XML 文件中

Now I want to save new data to this XML file in a similar manner (or any way that works really...)

我不知道如何做到这一点,我试过了,但它不起作用:

I have no idea how I could do this, I tried this but it is not working:

public async void saveToXML()
{
    var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
    _Folder = await _Folder.GetFolderAsync("Data");

    var _File = await _Folder.GetFileAsync("Names.xml");

    XmlDocument document = new XmlDocument();
    XmlElement root = document.CreateElement("names");
    document.AppendChild(root);

    XmlElement naam = document.CreateElement("name");
    naam.InnerText = "TestNaam2";
    root.AppendChild(naam);

    await Windows.Storage.FileIO.WriteTextAsync(_File, document.getXml());
}

当我尝试运行它时,我得到一个 UnauthorizedAccessException...(我知道以前的方法会覆盖,但这不会有问题,我只是希望它首先写入.)

When I try running this I get a UnauthorizedAccessException... (I am aware that the previous method will overwrite, but that wouldn't be a problem, I just want it to write in the first place.)

我做错了什么,我该怎么做?Windows 8 应用程序很难读写 XML...

What am I doing wrong and how should I go about doing this? Windows 8 Apps are quiet difficult for reading and writing XML...

谢谢!

推荐答案

也许是文件夹的访问权限

Maybe the access to the folder

var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;

受到限制.尝试以管理员身份运行您的应用.

is restricted. Try to run your app as administrator.

或者甚至更好地尝试为您的应用使用本地存储文件夹:

Or even better try to use the local storage folder for your app:

var _Folder = Windows.Storage.ApplicationData.Current.LocalFolder;


您在 Visual Studio 项目中看不到 LocalFolder.但是您可以随应用程序一起提供默认 XML 文件,如果它不存在,则将其移动到 LocalFolder.让我们创建一个辅助函数:

You cannot see the LocalFolder in your Visual Studio project. But you can ship the default XML file with your app and move it to LocalFolder if it doesn't exist there. Let's create a helper function:

public async System.Threading.Tasks.Task<Windows.Storage.StorageFile> GetLocalFile(string folderName, string fileName)
{
    var appLocalFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
    var appInstallLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;

    var dataFolder = await appLocalFolder.TryGetItemAsync(folderName) as Windows.Storage.StorageFolder;

    if (dataFolder == null)
    {
        // the folder with given name does not exist in ApplicationData.Current.LocalFolder
        // so we have to create it
        dataFolder = await appLocalFolder.CreateFolderAsync(folderName);
    }

    var localFile = await dataFolder.TryGetItemAsync(fileName) as Windows.Storage.StorageFile;

    if (localFile == null)
    {
        // local file does not exist - so we copy it from application install folder to
        // our local data folder
        var installDataFolder = await appInstallLocation.GetFolderAsync(folderName);
        var sourceFile = await installDataFolder.GetFileAsync(fileName);
        localFile = await sourceFile.CopyAsync(dataFolder);
    }

    return localFile; 
}

只需替换您的 readFromXML() 函数的这一部分:

And just replace this part of your readFromXML() function:

var _Folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
_Folder = await _Folder.GetFolderAsync("Data");

var _File = await _Folder.GetFileAsync("Names.xml");

调用我们的新辅助函数:

With the call to our new helper function:

var _File = await GetLocalFile("Data", "Names.xml");

这篇关于将 XML 写入文件时出现 UnauthorizedAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆