【问题标题】:Save and XDocument to a file in a "using stream"将 XDocument 保存到“使用流”中的文件
【发布时间】:2016-05-07 13:11:53
【问题描述】:

所以我正在尝试执行打开 XML 文件、向其中添加和 XElement 并保存它的简单任务。我的当前代码不起作用:

Uri dataUri = new Uri("ms-appx:///XML Files/Pinouts.xml");
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
using (var stream = await file.OpenStreamForWriteAsync())
{
    XDocument doc = XDocument.Load(stream);
    doc.Add(new XElement(XElement.Parse(CurrentPinOut)));
    doc.Save(stream);
    stream.Flush();
}

当我收到“拒绝访问”错误时。我猜这是一个简单的修复,但我在搜索的最后一个小时内没有找到任何东西。有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    您不能将文件写入 InstalledLocation - 它是只读,但您肯定可以将文件写入 LocalFolder(或其他可访问的位置) :

    StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("Pinouts.xml", CreationCollisionOption.GenerateUniqueName);
    using (var stream = await file.OpenStreamForWriteAsync())
    {
        XDocument doc = XDocument.Load(stream);
        doc.Add(new XElement(XElement.Parse(CurrentPinOut)));
        doc.Save(stream);
        stream.Flush();
    }
    

    您可以在at MSDN找到有关应用程序数据的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多