c# – How to include an XML file inside the executable of my project?

Question:

I have created a project in Visual Studio 2012 C# . I have added an .xml file and this file contains information that the console reads when it is run.

I need to publish it so I can run it and give it to the people who asked me to do this.

Is that the .exe file can be found in the debug directory, but this does not help me because when I run it on another computer it says that it is missing files related to the console (the xml file)

When I have published the console, it gives me some files in the release directory, but it does not include the xml and I do not have to include it to make it visible since it contains private information.

How could I do this?

XML file syntax:

<root>
    <url></url>
</root>

In the code I am using XmlDocument with this I load my xml file by calling it by tag, that is GetElementsByTagName , and thus I reference the tag that I want to call inside a for loop.

Answer:

Assuming your IDE is Visual Studio, then you can embed the xml file in the .exe as a resource.

In the properties of the project you want to add, go to the Recursos page / Agregar recurso / add archivo existente. (this differs from the language the IDE is configured in)

Once the resource is loaded you will be able to view it in the Properties folder.

One of the ways to load the file by code is this:

XElement resource = XElement.Parse(Properties.Resources.MiArchivoCargado)

Where MiArchivoCargado is the file you previously uploaded!

Scroll to Top