If you need to use an XML file in your C# project let us show you how with our simple how to guide.
For programming, it can be used in a variety of different ways. Such as reading or writing data via an application. I’ve used them many times when I’ve created applications using Visual Studio.
This guide will show you two ways of adding the XML File. You can either do a data first approach or a file first approach. The video above shows the file first approach and is the simplest. Read on below to take a look at the other option.
Table Of Contents:
Creating an XML File
Example XML
Add XML File to C# Project in Visual Studio
XML File F.A.Q’s
Creating an XML File
Creating a new XML file and populating it with data is really simple. All you need is a simple code editor (I use Visual Studio Code or Notepad ++) and a spreadsheet program such as Microsoft Excel.
The reason I use Excel is to save time. XML files are structured in a way that uses parent – child nodes. If you have many nodes the file can become quite large.
I’ve recently created an XML file using a list of over 200 email addresses. Excel can be used to automatically generate the file so you don’t have to manually wrap your data in the nodes.
Example XML:
<competition>
<entrant>
<name>Mike</name>
<email>hello@codewithmike.com</email>
<optinstatus>Y</optinstatus>
</entrant>
<entrant>
<name>James</name>
<email>newsletter@codewithmike.com</email>
<optinstatus>N</optinstatus>
</entrant>
</competition>
As you can see in the example above, the overall file will be used for a competition.
In the below step by step guide we’ll be creating an XML file for competition entries. Each entry will have the name, email address and newsletter opt-in status. You can follow the same process for what ever data your XML file will contain.
Step 1: Open Excel & Add XML Formatting
Open up a new excel worksheet and add the following:

Step 2: Paste in your list
Paste in your list of data. In our example it’s names, emails and optin status. It should look like this:

Step 3: Concatenate the cells
Using an Excel formula we can generate 99% of the XML file, saving us loads of time.
– Click inside cell D1.
– Type =CONCATENATE(A1,B1,C1)
– Press Enter
It should look like the below:

Double click the little black dot in the bottom right hand corner to apply the formula to the rest of the cells.

Your Excel sheet should now resemble some formatted XML, similar to below:

Step 4: Saving as .xml
– Open up your code editor
– Add the Parent Nodes
– Copy the results from Row D in your Excel sheet
– Paste into your code editor between the parent nodes
– Save your file as .xml
It should look like this:

Now all we need to do is add the xml to our visual studio solution.
Add the XML file to Visual Studio Project
Open up Visual Studio and create a new project or open an existing one.
– On the right hand side right click your project name
– Click Add
– Click Existing Item

– Select the XML file we created earlier and press Add.
– The file will be added to the project.

– Open the file to check the contents.
– You can now work with the XML file that you added to Visual Studio.

XML File F.A.Q’S
You can use the XDocument class to work with XML files in C#. Example: var xdoc = XDocument.Load(PATH_TO_FILE);
A xml file is an Extensible Markup Language (XML) file. Basically they are just plain text files using custom tags to describe the structure and data that’ll be held inside the file.
XML files are used by programs, devices and applications to handle, structure, store, transmit, and display plain text data.
XML stores plain text data.