3

I have added an XML file in my project and I am using it as database. I have set it as embedded resource. Everything works fine till I am in my development environment.

But when I publish it and install it in my machine, the XML file doesn't get copied to the installation path automatically and so it starts giving error that it cannot find the file.

What is the correct way to use XML file to store and retrieve data in a windows application and what am I am doing wrong?

Thanks in advance!

Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105
  • What is the error? And post the code that “cannot find the file”. – Dour High Arch Feb 26 '14 at 21:28
  • Its just give the error that it cannot find the file in the path and give the path of the installation. And this is the simplet code I am using to read the file `XDocument doc = XDocument.Load("MasterData.xml");` – Pawan Nogariya Feb 26 '14 at 21:54
  • @DourHighArch - Here is the exact error `System.IO.FileNotFoundException: Could not find file 'C:\Users\User\AppData\Local\Apps\2.0\PV8CYYPD.1XE\GZQP92CW.WWV\code..tion_0000000000000000_0001.0000_a42745f5ded40bd9\MasterData.xml'. File name: 'C:\Users\User\AppData\Local\Apps\2.0\PV8CYYPD.1XE\GZQP92CW.WWV\code..tion_0000000000000000_0001.0000_a42745f5ded40bd9\MasterData.xml' at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)` – Pawan Nogariya Feb 26 '14 at 22:08

2 Answers2

1

The whole point of embedded resource is that it isn't copied to the output directory, but rather is embedded within the executable. The quickest solution would be to alter your logic to read from the assembly (note that this assumes it's embedded in the same assembly as the executable):

using (Stream stream = this.GetType().Assembly.GetManifestResourceStream(filename)) // Needs to account for folder paths, etc
{
    using (StreamReader sr = new StreamReader(stream))
    {
        string xml = sr.ReadToEnd();

        // Do stuff
    }
}

EDIT: This SO question covers the topic nicely, with essentially the same code (great minds...), but a better explanation of how to use it and what you need to watch out for.

Community
  • 1
  • 1
Matt
  • 2,682
  • 1
  • 17
  • 24
  • Thanks! I am able to get the file like this and can read it but I can't make any changes to the file content. It allows me to change the content and save it but when I fetch it, my changes got lost – Pawan Nogariya Mar 03 '14 at 21:22
  • Since embedded resources are compiled into your assembly, you can't edit them. If you want to make changes to it, I would use a different resource type (i.e. `Content`) and set "Copy to Output Directory" to "Copy Always" or "Copy if Newer" – Matt Mar 03 '14 at 21:27
  • When I do this it starts giving same exception, `FileNoFoundException` :( – Pawan Nogariya Mar 04 '14 at 18:41
  • Have you verified that your deployment project (or ClickOnce) is properly including and copying the file? – Matt Mar 04 '14 at 18:45
  • I am doing it through "Publish" option of visual studio. I am not sure how I can check if it is included the content files or not. Can you tell me how I can verify this? – Pawan Nogariya Mar 04 '14 at 18:56
1

Found the solution!

What I did is, made my xml file resource type Content and set Copy to Output Directory to Copy Always to make it editable as per @tencntraze comment. However, it was still throwing the same exception when I was trying to read it FileNotFoundException once I publish and install it.

Then I found the main trick, I went to

Properties > Publish (tab) > Application Files (Button) > Changed Publish Status for my file from Data File to Include

and it worked!

Thanks!

Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105