DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Displaying and Filtering XML Data with WinForms in VB

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. VB 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 Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
loadCombo()
End Sub

Private Sub loadCombo()
ComboBox1.Items.Clear()

Dim doc As New XmlDocument()
doc.Load("XMLFile.xml")
Dim nodeList As XmlNodeList = doc.SelectNodes("Persons/Person")

For Each node As XmlNode In nodeList
If (Not ComboBox1.Items.Contains(node.SelectSingleNode("City").InnerText)) Then
ComboBox1.Items.Add(node.SelectSingleNode("City").InnerText)
End If
Next node
End Sub

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 Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xmlDoc As XDocument = XDocument.Load("XMLFile.xml")

Dim persons = From person In xmlDoc.Descendants("Person") _
Select Name = person.Element("Name").Value, City = person.Element("City").Value, _
Age = person.Element("Age").Value

richTextBox1.Text = ""
For Each person In persons
richTextBox1.Text = richTextBox1.Text & "Name: " & person.Name + Constants.vbLf
richTextBox1.Text = richTextBox1.Text & "City: " & person.City + Constants.vbLf
richTextBox1.Text = richTextBox1.Text & "Age: " & person.Age + Constants.vbLf + Constants.vbLf
Next person

If richTextBox1.Text = "" Then
richTextBox1.Text = "No Results."
End If
End Sub

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

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 Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim xmlDoc As XDocument = XDocument.Load("XMLFile.xml")

Dim persons = From person In xmlDoc.Descendants("Person") _
Where person.Element("City").Value = ComboBox1.SelectedItem.ToString() _
Select Name = person.Element("Name").Value, City = person.Element("City").Value, _
Age = person.Element("Age").Value

richTextBox1.Text = ""
For Each person In persons
richTextBox1.Text = richTextBox1.Text & "Name: " & person.Name + Constants.vbLf
richTextBox1.Text = richTextBox1.Text & "City: " & person.City + Constants.vbLf
richTextBox1.Text = richTextBox1.Text & "Age: " & person.Age + Constants.vbLf + Constants.vbLf
Next person

If richTextBox1.Text = "" Then
richTextBox1.Text = "No Results."
End If
End Sub

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

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:

Imports System.Xml

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
loadCombo()
End Sub

Private Sub loadCombo()
ComboBox1.Items.Clear()

Dim doc As New XmlDocument()
doc.Load("XMLFile.xml")
Dim nodeList As XmlNodeList = doc.SelectNodes("Persons/Person")

For Each node As XmlNode In nodeList
If (Not ComboBox1.Items.Contains(node.SelectSingleNode("City").InnerText)) Then
ComboBox1.Items.Add(node.SelectSingleNode("City").InnerText)
End If
Next node
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xmlDoc As XDocument = XDocument.Load("XMLFile.xml")

Dim persons = From person In xmlDoc.Descendants("Person") _
Select Name = person.Element("Name").Value, City = person.Element("City").Value, _
Age = person.Element("Age").Value

richTextBox1.Text = ""
For Each person In persons
richTextBox1.Text = richTextBox1.Text & "Name: " & person.Name + Constants.vbLf
richTextBox1.Text = richTextBox1.Text & "City: " & person.City + Constants.vbLf
richTextBox1.Text = richTextBox1.Text & "Age: " & person.Age + Constants.vbLf + Constants.vbLf
Next person

If richTextBox1.Text = "" Then
richTextBox1.Text = "No Results."
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim xmlDoc As XDocument = XDocument.Load("XMLFile.xml")

Dim persons = From person In xmlDoc.Descendants("Person") _
Where person.Element("City").Value = ComboBox1.SelectedItem.ToString() _
Select Name = person.Element("Name").Value, City = person.Element("City").Value, _
Age = person.Element("Age").Value

richTextBox1.Text = ""
For Each person In persons
richTextBox1.Text = richTextBox1.Text & "Name: " & person.Name + Constants.vbLf
richTextBox1.Text = richTextBox1.Text & "City: " & person.City + Constants.vbLf
richTextBox1.Text = richTextBox1.Text & "Age: " & person.Age + Constants.vbLf + Constants.vbLf
Next person

If richTextBox1.Text = "" Then
richTextBox1.Text = "No Results."
End If
End Sub
End Class

Looking for the C# 2005 Version? Click Here!

Looking for more .NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!