-1

I'm using Visual Studio 2015. I have a XML file in my solution Xml/file.xml

I wrote:

XElement root = XElement.Load(@"Xml\file.xml");

My file.xml properties are:

Build Action = Content
Copy to Output Directory = Copy Always

In debug mode it works, but when I pubblish the solution, it searches the file in:

c:\windows\system32\inetsrv\Xml\file.xml

What is the solution?

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MarioProject
  • 417
  • 4
  • 25
  • You can [determine the actual path (depending on what app type this is)](http://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path) – stuartd Feb 03 '17 at 14:18
  • 1
    When you publish, you are changing your exe's location. Your root directory also changes to this new location. Move your file to the path its looking for or set an [absolute path](http://stackoverflow.com/questions/4796254/relative-path-to-absolute-path-in-c) for the file. – Ethilium Feb 03 '17 at 14:23

1 Answers1

1

If the file size isn't huge, I would personally usually do something like this as an embedded resource so that the XML is included inside your compiled code, and you don't need to load an external file ....

Set the properties:

Build Action = EmbeddedResource

and then use code something like this:

// adapt as needed
string yourResourceName = "YourAssembly.Xml.File.Xml";

// load the embedded resource
Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(yourResourceName);

// if resource is found
if (resourceStream != null) {
    tXElement root = XElement.Load(resourceStream);
    // do whatever you want to do ....
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459