简体   繁体   中英

C# Visual Studio 2015 load xml inside solution

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

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 ....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM