c# – How to include an XML file within 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 post it so I can run it and give it to the people who asked me to do this.

It 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 also do not have to include it to be visible as 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 calling it by tag, that is GetElementsByTagName , and thus I reference the tag that I want to call inside a for loop.

Answer:

Assuming the IDE you run 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 it to, go to the Recursos / Agregar recurso / add the archivo existente. (this differs from the language in which the IDE is configured)

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