Working With XML Files in C#
This is a basic tutorial which will show you how to write a C# program to work with XML files, we will be using the .NET module
System.Xml.
For this tutorial, we will pretend we have been hired to create a program that will read xml files with specifications of your emplorer's computers. The XML files are structured like this:
xml
<?xml version="1.0"?>
<CompSpecs>
<CP>
<Type>Dell Dimension 6600</Type>
<RAM>2GBs</RAM>
<CPU_Speed>2.8Ghz</CPU_Speed>
</CP>
</CompSpecs>
To start, create a C# windows Forms Application, and create a form as shown below:

*Note: by default, the three textboxes should be named textBox1, textBox2, textBox3.
As you can see, we will load three items for each computer, Type, Amount of RAM, and the CPU Speed.
Now add an "open file dialog" to our project, to allow the user to open a "computer specifications xml file".
To get started, we will set up the loading part of our program, go into code view of Form1.h, and add the following using references:
csharp
using System.Xml;
using System.IO;
Reading and parsing xml contentTo read and parse the xml file, we will:
- Create a file stream
- Create an XmlDocument that will read the filestream
- Parse the XmlDocument
- Display the InnerText of the Nodes with the needed information in it
First, go back into design mode, and double-click the "Load" Button, to create an
onclick() event. In the newly created event, insert this code:
csharp
System.Windows.Forms.DialogResult OPEN = openFileDialog1.ShowDialog();
if (OPEN == DialogResult.OK)
{
string path = openFileDialog1.FileName; // The Path to the .Xml file //
FileStream READER = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //Set up the filestream (READER) //
System.Xml.XmlDocument CompSpecs = new System.Xml.XmlDocument();// Set up the XmlDocument (CompSpecs) //
CompSpecs.Load(READER); //Load the data from the file into the XmlDocument (CompSpecs) //
System.Xml.XmlNodeList NodeList = CompSpecs.GetElementsByTagName("CompSpecs"); // Create a list of the nodes in the xml file //
textBox1.Text = NodeList[0].FirstChild.ChildNodes[0].InnerText; // Load Type //
textBox2.Text = NodeList[0].FirstChild.ChildNodes[1].InnerText; // Load RAM //
textBox3.Text = NodeList[0].FirstChild.ChildNodes[2].InnerText; // Load CPU Speed //
}
We can now run our program and click the "Load" Button, the program should then load the above mentioned xml file, and output the data like this:

Now, that was simple enough wasn't it!
Changing and saving xml contentWe will now need to create the Save function of our program. To edit and save the xml contents, we will:
- Open a filestream
- Load the contents of the filestream into an XmlDocument
- Parse the XmlDocument
- Edit the InnerText of the nodes that are being edited
- Save the xmlDocument
To do this, create an "onClick()" event for our "Save" Button, and type in the code below in the newly created function:
csharp
if (openFileDialog1.FileName.Length > 0) //If a file is open
{
string path = openFileDialog1.FileName; // The Path to the .Xml file //
FileStream READER = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //Set up the filestream (READER) //
System.Xml.XmlDocument CompSpecs = new System.Xml.XmlDocument();// Set up the XmlDocument (CompSpecs) //
CompSpecs.Load(READER); //Load the data from the file into the XmlDocument (CompSpecs) //
System.Xml.XmlNodeList NodeList = CompSpecs.GetElementsByTagName("CompSpecs"); // Create a list of the nodes in the xml file //
NodeList[0].FirstChild.ChildNodes[0].InnerText = textBox1.Text; // Save the type
NodeList[0].FirstChild.ChildNodes[1].InnerText = textBox2.Text; // Save the RAM
NodeList[0].FirstChild.ChildNodes[2].InnerText = textBox3.Text; // Save the CPU Speed
//Save the xml file
//Create a FileStream for writing
FileStream WRITER = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite); //Set up the filestream (READER) //
//Write the data to the filestream
CompSpecs.Save(WRITER);
}
There we have it, the above code will allow us to change the xml file's attributes to match the ones the user has entered!
This is the end of this tutorial, I hope it was useful to you! Thanks for reading!
- AJ32

I have finished writing part two of this XML tutorial:
Working with xml files in C# - Part 2
This post has been edited by aj32: 16 Apr, 2008 - 05:58 PM