保存并到的XDocument在文件中的"采用流" [英] Save and XDocument to a file in a "using stream"
本文介绍了保存并到的XDocument在文件中的"采用流"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我试图做一个打开XML文件的简单的任务,添加和的XElement它,保存它。我CURENT代码不起作用:
乌里dataUri =新的URI(MS-APPX:/// XML文件/针脚的.xml);
StorageFile文件=等待StorageFile.GetFileFromApplicationUriAsync(dataUri);使用
(VAR流=等待file.OpenStreamForWriteAsync())
{
的XDocument DOC = XDocument.Load(流);
doc.Add(新的XElement(XElement.Parse(CurrentPinOut)));
doc.Save(流);
stream.Flush();
}
当我得到一个拒绝访问错误。我猜这是一个简单的解决,但我没有找到搜索的最后一个小时什么。 ?任何想法
解决方案
您不能写入文件的 InstalledLocation 的 - 它的只读的,但你一定可以写文件的 LocalFolder 的(或者其他方便的地点):
<预类=郎-CS prettyprint-覆盖 >
StorageFile文件=等待ApplicationData.Current.LocalFolder.CreateFileAsync(Pinouts.xml,CreationCollisionOption.GenerateUniqueName);使用
(VAR流=等待file.OpenStreamForWriteAsync())
{
的XDocument DOC = XDocument.Load(流);
doc.Add(新的XElement(XElement.Parse(CurrentPinOut)));
doc.Save(流);
stream.Flush();
}
有关应用程序数据的更多信息,你会发现的在MSDN 。
So I'm trying to do the simple task of opening an XML file, adding and XElement to it, and saving it. My curent code doesn't work:
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();
}
As I get an "Access denied" error. I'm guessing it's a simple fix, but I haven't found anything in the last hour of searching. Any ideas?
解决方案
You cannot write files to InstalledLocation - it's read-only, but you surely can write files to LocalFolder (or other accessible locations):
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();
}
More info about application data you will find at MSDN.
这篇关于保存并到的XDocument在文件中的"采用流"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文