Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 99,790 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,579 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Working with XML files in C#

 
Reply to this topicStart new topic

> Working with XML files in C#

aj32
Group Icon



post 12 Apr, 2008 - 08:55 PM
Post #1


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:

IPB Image

*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 content

To read and parse the xml file, we will:
  1. Create a file stream
  2. Create an XmlDocument that will read the filestream
  3. Parse the XmlDocument
  4. 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:

IPB Image



Now, that was simple enough wasn't it!


Changing and saving xml content

We will now need to create the Save function of our program. To edit and save the xml contents, we will:
  1. Open a filestream
  2. Load the contents of the filestream into an XmlDocument
  3. Parse the XmlDocument
  4. Edit the InnerText of the nodes that are being edited
  5. 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 smile.gif

I have finished writing part two of this XML tutorial: Working with xml files in C# - Part 2 smile.gif

This post has been edited by aj32: 16 Apr, 2008 - 06:58 PM


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 7/25/08 01:48AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->