This tutorial will show you how to create a Windows Application that can read the contents of an XML file, and also how to filter the contents of an XML file to display only the data we want. C# version.
WinForms are the Windows Applications we can create in Visual Studio. In this tutorial, we will explore how we can use an XML file to store data and also use a WinForm to extract this data and display it on our form, as well as filtering the data so we can view just what we want to.
The first thing we will do is to create a sample XML file, or if you have one already you can use that one.
We will create an XML file with people's names, city and age. The XML file will look like this:
<?xml version="1.0" encoding="utf-8" ?> <Persons>
<Person>
<Name>Paxton</Name> <City>Munich</City> <Age>29</Age> </Person> <Person>
<Name>Mike</Name> <City>Orlando</City> <Age>33</Age> </Person> <Person>
<Name>Ella</Name> <City>LA</City> <Age>13</Age> </Person> <Person>
<Name>Zach</Name> <City>Munich</City> <Age>32</Age> </Person> <Person>
<Name>Ingrid</Name> <City>Oslo</City> <Age>63</Age> </Person> </Persons> |
We can now start designing our Form. We are going to implement two buttons, a ComboBox, a RichTextBox, and a label. One button will retrieve all the data from the XML file and display it in the RichTextBox, the other button will retrieve the data that matches our ComboBox and display it in the RichTextBox, and the ComboBox will display all the Cities in the XML file, so we can filter the data by City. The form will look something like this:
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
Once we have our form designed, we can double-click on a blank area of the form to create a Form_Load event in the code-behind, and we will create a method for the ComboBox to be populated. The code for this method looks something like this:
private void Form1_Load(object sender, EventArgs e) {
loadCombo(); }
private void loadCombo() {
comboBox1.Items.Clear(); XmlDocument doc = new XmlDocument(); doc.Load("XMLFile.xml"); XmlNodeList nodeList = doc.SelectNodes("Persons/Person"); foreach(XmlNode node in nodeList)
if (!comboBox1.Items.Contains(node.SelectSingleNode("City").InnerText))
comboBox1.Items.Add(node.SelectSingleNode("City").InnerText); } |
This method loads our XML file and selects the node that our data (City) is at. Then, for each node in our selected node list, we will add the City item to our ComboBox - but only if it has not already been added. This will avoid duplicates in the ComboBox.
Next, we can double-click our first button in design view to write the code for that. We will want to select all the data from the XML file and output it to the RichTextBox. The code for this button will look something like this:
private void button1_Click(object sender, EventArgs e) {
XDocument xmlDoc = XDocument.Load("XMLFile.xml"); var persons = from person in xmlDoc.Descendants("Person")
select new {
Name = person.Element("Name").Value, City = person.Element("City").Value, Age = person.Element("Age").Value, }; richTextBox1.Text = ""; foreach (var person in persons) {
richTextBox1.Text = richTextBox1.Text + "Name: " + person.Name + "\n"; richTextBox1.Text = richTextBox1.Text + "City: " + person.City + "\n"; richTextBox1.Text = richTextBox1.Text + "Age: " + person.Age + "\n\n"; } if (richTextBox1.Text == "")
richTextBox1.Text = "No Results."; } |
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
This block of code loads our XML file, then we use LINQ to select all the data from within, then simply loop through the data and output it to our RichTextBox. The \n escape sequence will output a new line in our text box.
We can use a similar block of code for our other button, but instead of selecting all the data from the XML file, we will use a WHERE clause in the LINQ code to select only the XML data that matches our requirements (our selected item from the ComboBox). The code will look like this:
private void button2_Click(object sender, EventArgs e) {
XDocument xmlDoc = XDocument.Load("XMLFile.xml"); var persons = from person in xmlDoc.Descendants("Person")
where person.Element("City").Value == comboBox1.SelectedItem.ToString() select new {
Name = person.Element("Name").Value, City = person.Element("City").Value, Age = person.Element("Age").Value, }; richTextBox1.Text = ""; foreach (var person in persons) {
richTextBox1.Text = richTextBox1.Text + "Name: " + person.Name + "\n"; richTextBox1.Text = richTextBox1.Text + "City: " + person.City + "\n"; richTextBox1.Text = richTextBox1.Text + "Age: " + person.Age + "\n\n"; } if (richTextBox1.Text == "")
richTextBox1.Text = "No Results."; } |
As you can see, this code block is identical with the exception of the WHERE clause in the LINQ Select statement. We are only selecting the data which matches our selection from the ComboBox. Now if we run this application, we will be able to load the entire XML file into the textbox, and also filter the data by our Combo Box.
The entire code-behind is as follows:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.Linq;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) {
loadCombo(); } private void loadCombo() {
comboBox1.Items.Clear(); XmlDocument doc = new XmlDocument(); doc.Load("XMLFile.xml"); XmlNodeList nodeList = doc.SelectNodes("Persons/Person"); foreach(XmlNode node in nodeList)
if (!comboBox1.Items.Contains(node.SelectSingleNode("City").InnerText))
comboBox1.Items.Add(node.SelectSingleNode("City").InnerText); } private void button1_Click(object sender, EventArgs e) {
XDocument xmlDoc = XDocument.Load("XMLFile.xml"); var persons = from person in xmlDoc.Descendants("Person")
select new {
Name = person.Element("Name").Value, City = person.Element("City").Value, Age = person.Element("Age").Value, }; richTextBox1.Text = ""; foreach (var person in persons) {
richTextBox1.Text = richTextBox1.Text + "Name: " + person.Name + "\n"; richTextBox1.Text = richTextBox1.Text + "City: " + person.City + "\n"; richTextBox1.Text = richTextBox1.Text + "Age: " + person.Age + "\n\n"; } if (richTextBox1.Text == "")
richTextBox1.Text = "No Results."; } private void button2_Click(object sender, EventArgs e) {
XDocument xmlDoc = XDocument.Load(Application.StartupPath + "/XMLFile.xml"); var persons = from person in xmlDoc.Descendants("Person")
where person.Element("City").Value == comboBox1.SelectedItem.ToString() select new {
Name = person.Element("Name").Value, City = person.Element("City").Value, Age = person.Element("Age").Value, }; richTextBox1.Text = ""; foreach (var person in persons) {
richTextBox1.Text = richTextBox1.Text + "Name: " + person.Name + "\n"; richTextBox1.Text = richTextBox1.Text + "City: " + person.City + "\n"; richTextBox1.Text = richTextBox1.Text + "Age: " + person.Age + "\n\n"; } if (richTextBox1.Text == "")
richTextBox1.Text = "No Results."; } } } |
Looking for the VB.NET 2005 Version? Click Here!
Looking for more .NET Tutorials? Click Here!