简体   繁体   English

使用Metro应用程序Windows 8阅读XML文件“我的文档”

[英]Read XML file fom “My documents” with metro app Windows 8

I'm having an issues with a reading xml file from "my documents" folder. 我在从“我的文档”文件夹中读取xml文件时遇到问题。 I created a xml file in the folder Data inside "my documents". 我在“我的文档”中的“数据”文件夹中创建了一个xml文件。 I have changed already the Capabilities of the app but at the XmlReader reader line, there is an exception triggered "Access to the path 'C:\\Users...\\Documents\\DomusGest\\DomusGestFile.xml' is denied." 我已经更改了应用程序的功能,但是在XmlReader阅读器行中,触发了一个异常“拒绝访问路径'C:\\ Users ... \\ Documents \\ DomusGest \\ DomusGestFile.xml'”。

                StorageFolder storageFolder = null;
                StorageFile storageFile = null;
                storageFolder = await KnownFolders.DocumentsLibrary.GetFolderAsync("Data");
                storageFile = await storageFolder.GetFileAsync("DataXML.xml");
                string storageFilePath = storageFile.Path;


                XmlReader reader = XmlReader.Create(storageFilePath);
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "Apparmtment"))
                    {
                        if (reader.HasAttributes)
                            {
                                WriteXML.Text = reader.GetAttribute("name");//write on Win8 App
                            }
                    }
                }

With this same code, I can read the file if the file is located in a folder of the metro app solution. 使用相同的代码,如果文件位于Metro应用程序解决方案的文件夹中,则可以读取该文件。

What am I doing wrong? 我究竟做错了什么?

Thanks 谢谢

You need to go to Properties, capabilities and activate Document Library Access capability. 您需要转到属性,功能并激活文档库访问功能。 Then you need to update the app manifest declaring what type of document you want to te able to read ".xml" in your case; 然后,您需要更新应用清单,声明要在您的情况下能够读取“ .xml”的文档类型; this can be done from the declarations tab. 这可以从“声明”选项卡中完成。

This can be done, however, bear in mind this is not the best option for neither the user or you, the developer. 可以做到这一点,但是请记住,对于用户或开发人员而言,这都不是最佳选择。 For the user this is unsafe and will have to accept the capability, thing that may (though I doubt it) draw back some users. 对于用户而言,这是不安全的,必须接受该功能,这可能会(尽管我对此表示怀疑)会吸引一些用户。 On the other hand, the user may modify this XML easier (or another app) and you will have to deal with it. 另一方面,用户可以更轻松地(或另一个应用程序)修改此XML,您将不得不对其进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM