Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows8XML文件写入_C#_Xml_Exception_Windows 8 - Fatal编程技术网

C# Windows8XML文件写入

C# Windows8XML文件写入,c#,xml,exception,windows-8,C#,Xml,Exception,Windows 8,我在写入xml文件时遇到问题,该文件位于我的应用程序文件夹(windows 8,metro样式)中。当我试图以读/写模式打开文件时,我遇到了未经授权的AccessException。我做了很多研究,但还是一无所获。我尝试了这个解决方案: var sf = await Package.Current.InstalledLocation.GetFileAsync(@"data.xml"); XmlDocument xmlDoc; using (var stream =

我在写入xml文件时遇到问题,该文件位于我的应用程序文件夹(windows 8,metro样式)中。当我试图以读/写模式打开文件时,我遇到了未经授权的AccessException。我做了很多研究,但还是一无所获。我尝试了这个解决方案:

var sf = await Package.Current.InstalledLocation.GetFileAsync(@"data.xml");
        XmlDocument xmlDoc;
        using (var stream = await sf.OpenAsync(FileAccessMode.ReadWrite))
        {
            xmlDoc = await XmlDocument.LoadFromFileAsync(sf);
            XmlElement root = xmlDoc.DocumentElement;
            XmlElement xe = xmlDoc.CreateElement("debt");

            XmlElement id = xmlDoc.CreateElement("Id");
            id.InnerText = Guid.NewGuid().ToString();

            XmlElement name = xmlDoc.CreateElement("Name");
            name.InnerText = d.Name;

            XmlElement surname = xmlDoc.CreateElement("Surname");
            surname.InnerText = d.Surname;

            xe.AppendChild(id);
            xe.AppendChild(name);
            xe.AppendChild(surname);

            root.AppendChild(xe);
        }
        if (xmlDoc != null)
            await xmlDoc.SaveToFileAsync(sf);  
但在我打开流的行中再次出现异常


thx for your help

Package.Current.InstalledLocation.GetFileAsync表示应用程序的安装位置,这是一个无法直接写入文件的区域。使用以下命令

Windows.ApplicationModel.Package.Current.InstalledLocation

Windows.Storage.ApplicationData.Current.LocalFolder.Path


我使用的是前者,效果很好,请参见Package.Current.InstalledLocation的值。你试过以管理员的身份运行它吗?尽管这可能不是一个解决方案,但它可能是这里出了什么问题的一个指标。