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

13,473

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.

Share:
13,473
Shailesh Jaiswal
Author by

Shailesh Jaiswal

Updated on June 04, 2022

Comments

  • Shailesh Jaiswal
    Shailesh Jaiswal almost 2 years

    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();
                }
    

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

    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);
    

    I am getting error at

    doc = XDocument.Load(strXMLFile);
    

    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 ?

    • Olivier Payen
      Olivier Payen over 13 years
      Did you check that the "Build Action" is set to "Content" in the properties of your XML file?
  • WindyHen
    WindyHen over 9 years
    Tho i'm 4 years too late to comment, but it worked like a charm. Thanks.