将XML写入文件的UnauthorizedAccessException

时间:2014-02-24 16:13:13

标签: c# xml windows-8 windows-store-apps

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

xml看起来像这样:

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

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

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

这一切都有效,一个名字就可以读了。

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

我不知道我怎么能做到这一点,我试过这个,但它不起作用:

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 ...(我知道前面的方法会覆盖,但这不会有问题,我只想让它首先写。)

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

谢谢!

1 个答案:

答案 0 :(得分:1)

也许可以访问文件夹

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

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

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

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


更新

您无法在Visual Studio项目中看到LocalFolder。但是您可以使用您的应用程序发送默认XML文件,如果它不存在,则将其移动到LocalFolder。让我们创建一个辅助函数:

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()函数的这一部分:

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

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

调用我们的新辅助函数:

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