如何在Windows Phone 7中加载位于应用程序文件夹内的XML文件?

问题描述:

我正在开发Windows Phone 7应用程序.我是Window Phone 7应用程序的新手.我已通过右键单击项目&在项目中添加XML文件.选择添加->新项目.然后,通过使用以下代码,我可以轻松地在应用程序中加载XML文件

I am developing window phone 7 application. I am new to the window phone 7 application. I have added XML file in my project by right clicking the project & selecting the Add -> New Item. I can then easily load the XML file in my application by using the following code

IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication();
            XDocument doc = null;
            IsolatedStorageFileStream isfStream = null;
            if (isfData.FileExists(strXMLFile))
            {
                isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, isfData);
                doc = XDocument.Load(isfStream);
                isfStream.Close();
            }
            else
            {
                doc = XDocument.Load(strXMLFile);
                isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.CreateNew, isfData);
                doc.Save(isfStream);
                isfStream.Close();
            }

通过使用以上代码,我可以执行读取&在我的XML文件中进行写操作.

By using the above code I can perform the read & write operation in my XML file.

但是,当我将XML文件放入文件夹时,就会出现问题.我的问题如下: 通过在项目名称&上单击鼠标右键,在我的项目中添加了一个名为"XML文件"的文件夹.在Visual Studio中选择添加"->新建文件夹".然后,通过右键单击文件夹&文件夹,将XML文件添加到"XML文件"文件夹中.选择添加->新项目.当我将XML文件放入文件夹时,无法将其加载到应用程序中.我也尝试过以下语句

But the problem arises when I put my XML file into the folder. My problem is as follows: I have added one folder named 'XML Files' in my project by right clicking the project name & selecting the Add -> New Folder in the visual studio. Then I have added a XML file into the 'XML Files' Folder by right clciking the folder & selecting the Add->New Item. When I put the XML file into the folder I am not able to Load it in my application. I have also tried with the following statement

isfStream = new IsolatedStorageFileStream("/XML Files/"+strXMLFile, FileMode.Open, isfData);

我在

doc = XDocument.Load(strXMLFile);

我收到错误在应用程序xap软件包中找不到文件'/XML Files/A.xml'".我该怎么办 ?如何加载位于文件夹中的XML文件?我的代码有什么问题吗?您能否提供任何可解决上述问题的代码或链接?

I am getting the error "Cannot find file '/XML Files/A.xml' in the application xap package." What should I do ? How to load the XML file loacted inside folder ? Is anything wrong in my code ? Can you please provide me any code or link through which I can resolve the above issue ?

首先,确保将XML文件的Build Action设置为Content,并且将Copy to output选项设置为Copy if newer(或Copy always).然后,尝试以下方法:

First, make sure the Build Action for your XML file is set to Content and the Copy to output option is set to Copy if newer (or Copy always). Then, try this:

XDocument doc = XDocument.Load( "XML Files/MyXmlFile.xml" );

请注意,没有前导斜杠(/);几天前,我花了几个小时来解决这个愚蠢的问题.

Note that there is no leading forward slash (/); I spent a few hours a few days ago stuck on this silly problem.