cam
cam

Reputation: 9033

Adding XML Files to the Build

I'm using Visual Studio C# Express and I'm wondering how I would go about adding some XML files and being able to reference them in my code. I added the XML files in a folder under the project, but I'm not sure how I can reference them, and then get them copied to the output folder. Originally, before I added them, I just copied the XML files to the Debug folder for Visual Studio, but then when I compiled/installed a new copy of the program I had coded, I had to manually copy the XML files over.

Is there a way to add XML files to a Visual Studio Project and be able to reference them in the code and then have them copied to the output folder?

Upvotes: 9

Views: 23856

Answers (1)

Michal Ciechan
Michal Ciechan

Reputation: 13888

Right click project, Add Existing Resource, browse and select the file you want to add. Then right click the file and click properties and change "Build Action" to content, and "Copy To Output Directory" to Copy if newer (or copy always if the need be). Then you can access it by using the relative path.

I use this for my XML and I can access my content using the following code:

XmlDocument document = new XmlDocument();
document.Load("Resources/DefaultConfig.xml");

Please note that my DefaultConfig.xml file is inside a "Resoruces" Directory which I created in Visual Studio(this is optional, but it helps me keep my project neat)

Upvotes: 23

Related Questions