How to download the XML file located inside the application folder in the phone window 7?

I am developing an application for Windows Phone 7. I am new to Windows Phone 7. I added an XML file to my project by right-clicking the project and choosing Add → New Item. Then I can easily load the XML file in my application 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(); } 

Using the code above, I can perform a read and write operation in my XML file.

But the problem occurs when I put my XML file in a folder. My problem is this: I added one folder named "XML files" to my project, right-clicking on the project name and choosing Add → New Folder in the visual studio. Then I added the XML file to the XML Files folder by clicking on the folder and choosing Add> New Item. When I put the XML file in a folder, I cannot upload it to my application. I also tried with the following statement

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

I get an error

 doc = XDocument.Load(strXMLFile); 

I get an error "I can not find the file" / XML files /A.xml in the application xap package. "What should I do? How can I upload an XML file to a folder? Is there something wrong in my code? Can you provide me with some code or a link through which I can solve the above problem?

+4
source share
1 answer

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

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

Note that there is no leading slash (/); A few days ago I spent several hours stuck in this stupid problem.

+11
source

Source: https://habr.com/ru/post/1333461/


All Articles