Introduction
Topics regarding XML prove increasingly popular. It may be because XML is one of the latest “hot terms“; or it may be due to the complexity revolving around XML technologies. With this article I will show you three different ways to read and use XML documents productively in your VB.NET applications.
XML Parsing
There are essentially three main ways to read XML files. These are:
- SAX. Simple API (Application Programming Interface) for XML
- DOM API. Document Object Model Application Programming Interface
- XPath
SAX
SAX comes in very handy, especially when you have to read large XML files. With SAX the entire file is never in memory at the same time. It lets the built in parser decide which area of the file will be read and which will be placed in memory. Simple API for XML is a nice article on SAX.
DOM
There’s not much difference between the DOM API and the SAX API, except for the fact that with DOM, the whole file is read into memory, irrespective of the file’s size. So the limitation here is to not have great flexibility with reading large XML files. DOM and SAX is a slideshow about the DOM API.
XPath
XPath comes in very handy when reading XML files that are online. Wikipedia has more details on XPath.
Our Project
Create a new VB.NET Windows Forms application. You can name it anything you like. Design it to resemble Figure 1 below.
Figure 1 – Design
Before we start with the VB.NET project, let’s create an XML file to be used with this example. Open Notepad and edit it as follows:
<?xml version="1.0"?> <BLURays> <Movie Title="Fallen"> <Actor>Denzel Washington</Actor> <Type>Thriller</Type> </Movie> <Movie Title="Mad Max"> <Actor>Mel Gibson</Actor> <Type>Sci-Fi</Type> </Movie> <Movie Title="Event Horizon"> <Actor>Sam Neill</Actor> <Type>Sci-Fi Horror</Type> </Movie> <Movie Title="Happy Gilmore"> <Actor>Adam Sandler</Actor> <Type>Comedy</Type> </Movie> <Movie Title="The Postman"> <Actor>Kevin Costner</Actor> <Type>Adventure</Type> </Movie> </BLURays>
This is just a simple XML file containing some of my favorite movies and their respective actors and genre. You will be using this file throughout today’s example.
Code
In order to read any XML file and manipulate its data, we need certain namespaces. Add them now:
Imports System.Xml Imports System.Xml.XPath
The first namespace will help you manipulate XML files with either the SAX API or DOM API. The second namespace is used specifically with the XPath logic.
Reading XML Files with the SAX API
Add the following code behind the button labelled ‘SAX‘:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Use SAX ListBox1().Items.Clear() Dim xrReader As XmlReader = XmlReader.Create("BluRays.xml") Do While xrReader.Read() If xrReader.NodeType = XmlNodeType.Element AndAlso xrReader.Name = "Movie" Then ListBox1.Items.Add(xrReader.GetAttribute(0)) End If Loop End Sub
All that happens here is we read the BluRays.xml file with the help of an XMLReader object, then I specified the key it should place into the ListBox. In this case it will scan through the file and show each movie’s name inside the ListBox. This is quite a small file, so it was read piece by piece instead of the whole file at the same time.
Reading XML Files with the Use of the DOM API
Add the following code behind the button labelled ‘DOM‘:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'Use DOM ListBox1.Items.Clear() Dim xd As XmlDocument = New XmlDocument() xd.Load("BluRays.xml") Dim nlNodes As XmlNodeList = xd.GetElementsByTagName("Actor") For Each nNode As XmlNode In nlNodes ListBox1.Items.Add(nNode.InnerText) Next nNode End Sub
Here, with the aid of the XmlDocument object, you read the entire file and provide the Actors for each associated movie. The difference between SAX and DOM should be quite obvious now. With SAX I used the XmlReader object, which reads the file little by little, whereas with DOM I read the entire document into memory first, before providing the desired information.
Now the fun part starts!
Using XPath
XPath is best used with files that are online. Add the following code to the button labelled ‘XPath‘:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 'Use Xpath Dim xpdDoc As XPathDocument Dim nsManager As XmlNamespaceManager Dim xpNav As XPathNavigator Dim xpIterator As XPathNodeIterator Dim xpNode As XPathNavigator ' Create a new XmlDocument xpdDoc = New XPathDocument("http://xml.movies.ncc-cla.com/ns/rss/1.0/BluRays.xml") ' Create navigator xpNav = xpdDoc.CreateNavigator() ' Set up namespace manager for XPath nsManager = New XmlNamespaceManager(xpNav.NameTable) nsManager.AddNamespace("movies", "http://xml.movies.ncc-cla.com/ns/rss/1.0") ' Get forecast with XPath xpIterator = xpNav.Select("/rss/channel/item/movies:movies", nsManager) While xpIterator.MoveNext() xpNode = xpIterator.Current Console.WriteLine("{0}: {1}, {2}", _ xpNode.GetAttribute("Title", nsManager.DefaultNamespace), _ xpNode.GetAttribute("Actor", nsManager.DefaultNamespace), _ xpNode.GetAttribute("Type", nsManager.DefaultNamespace)) End While End Sub
Now, here I created a new XML Document object, and set it to URL path. Once I have that, I created a Navigator object to help locating the specific elements I need to find in my XML document. With the Iterator object I literally iterate through each XML attribute in the file and display them. Be advised though that this is just an example so proper implementation may differ than what I have shown you here.
Conclusion
I hope you have enjoyed today’s article. I’ll see you around!