XDocument.Save(字符串)不可用(XDocument.Save(string) not available)

编程入门 行业动态 更新时间:2024-10-23 15:17:28
XDocument.Save(字符串)不可用(XDocument.Save(string) not available)

我正在使用VS 2012 Express for Windows 8.我想加载XML文件,修改其内容然后将其保存回磁盘。

到目前为止,我一直在使用LINQ to XML,我已经能够加载文件,更改一些节点信息。

我想使用XDocument.Save(string)方法将文件保存回磁盘,但是intellisense不包括该方法,尽管它在联机文档中有记录。

知道为什么吗?

谢谢

--- UPDATE ---

这就是我想要做的

string questionsXMLPath; XDocument xmlDocQuestions = null; StorageFile file = null; public MainPage() { this.InitializeComponent(); questionsXMLPath = Path.Combine(Package.Current.InstalledLocation.Path, "Assets/Template.xml"); xmlDocQuestions = XDocument.Load(questionsXMLPath); } private async void SomeCodeHereToPopulateControls() { // This Code populates the controls on the Window to edit the XML nodes. } private async void Button_Click_3(object sender, RoutedEventArgs e) { XElement eleQuestion = (from el in xmlDocQuestions.Descendants("Question") where (string)el.Element("ID") == txtID.Text select el).FirstOrDefault(); eleQuestion.Elements("Description").FirstOrDefault().ReplaceWith(txtDescription.Text); xmlDocQuestions.Save(questionsXMLPath); // ERROR HERE AND CAN'T COMPILE }

I'm using VS 2012 Express for Windows 8.I want to load an XML file, modify its content and then save it back to disk.

Up to now, I've been using LINQ to XML and I've been able to load the file, change some node information.

I want to save the file back to the disk using the XDocument.Save(string) method but the intellisense does not include that method though it is documented in the online documentation.

Any idea why?

Thanks

---UPDATE---

Here's what I'm trying to do

string questionsXMLPath; XDocument xmlDocQuestions = null; StorageFile file = null; public MainPage() { this.InitializeComponent(); questionsXMLPath = Path.Combine(Package.Current.InstalledLocation.Path, "Assets/Template.xml"); xmlDocQuestions = XDocument.Load(questionsXMLPath); } private async void SomeCodeHereToPopulateControls() { // This Code populates the controls on the Window to edit the XML nodes. } private async void Button_Click_3(object sender, RoutedEventArgs e) { XElement eleQuestion = (from el in xmlDocQuestions.Descendants("Question") where (string)el.Element("ID") == txtID.Text select el).FirstOrDefault(); eleQuestion.Elements("Description").FirstOrDefault().ReplaceWith(txtDescription.Text); xmlDocQuestions.Save(questionsXMLPath); // ERROR HERE AND CAN'T COMPILE }

最满意答案

您需要使用Windows.Storage API 。 在Windows 8的沙盒和异步世界中,您会发现在处理文件存储方面存在两个显着差异:

应用程序可以以编程方式仅从该应用程序的“本地存储”访问数据,除非最终用户已授予应用程序特定权限,以便从文件系统中的其他位置存储/读取(通过文件和文件夹选取器 )

读取和写入文件是一种异步操作,因此您将找到大多数以“异步”结尾的文件访问方法,并且您将使用(通常) async / await模式来利用它们

有关详细信息以及文件访问示例 ,请查看 Windows开发人员中心的Windows 应用商店应用中的文件访问权限和权限主题 。

在您的特定情况下,您最终将使用上面引用的文章和代码示例中显示的技术将XDocument.ToString()写入所需的输出文件。

顺便说一下,对于学习文件系统(以及Windows Store编程特有的其他概念)的更全面和更有效的方法, (免费)Microsoft App Builder程序是一个很好的方法。

You'll need to use the Windows.Storage APIs. In the sandboxed and asynchronous world of Windows 8, you'll find two significant differences in dealing with file storage:

The application can programmatically access data only from the "local storage" for that application UNLESS the end-user has granted specific permission to the app to store/read from elsewhere in the file system (via the file and folder pickers)

Reading and writing to files is an asynchronous operation, so you'll find most of the file access methods ending in "Async" and you'll use (typically) the async/await pattern to leverage them

Take a look at the File access and permissions in Windows Store apps topic on the Windows Dev Center for more details as well as the File access sample.

In your specific case, you'll just end up writing XDocument.ToString() to the desired output file using the techniques shown in the article and the code sample cited above.

By the way, for a more holistic and measured approach to learning about the file system (as well as other concepts unique to Windows Store programming), the (free) Microsoft App Builder program is a great way to spin up.

更多推荐

本文发布于:2023-08-07 13:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464229.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   不可用   XDocument   Save   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!