Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 618
cam Avatar asked Feb 24 '10 13:02

cam


People also ask

Where do you put XML files?

First, you need to go to Files and Folders page in the folder you want to install the XML file in and use the import XML option to add it to the project. This option is available both on the toolbar and in the files pane context menu.

How to Add XML file in c#?

Add the XML file to Visual Studio ProjectOpen up Visual Studio and create a new project or open an existing one. Add an existing item to the C# project. – Select the XML file we created earlier and press Add. – The file will be added to the project.


1 Answers

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)

like image 195
Michal Ciechan Avatar answered Sep 30 '22 12:09

Michal Ciechan