Reading and Transforming XML Documents using Visual Basic 2005
This article mainly focuses on reading XML documents in several ways using Visual Basic.NET 2005. Topics discussed include the XMLReader and XSLT, among others.
The entire source code for this article is available in the form of a downloadable zip file. The solution was developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft Windows Server 2003 Enterprise Edition together. I didn’t really test the solution with any other/previous editions. If you have any problems in executing the solution, please post in the discussion area.
XML Schema and XML document
An XML document is simply a text file with some hierarchical and structural user-defined tags (like HTML tags) containing user-provided data. The syntax, grammar, rules, structure, hierarchy, constraints etc. of a particular XML document is defined using XML Schema (which is also an XML document, but with the XSD extension).
To work with this article, I worked with the following XML document (Employee.xml):
In the above code, I used a class called “XMLReader” available in the “System.XML” namespace. You should always use the “create” method existing in “XMLReader” class whenever you want to create an object related to the “XMLReader.” When the above code gets executed, you will receive the following output:
Employees
Employee
Empno
Ename
Sal
Deptno
.
.
.
Let us modify the above code in such a way that it gives the tags along with indentation. You can perfectly indent the tags if and only if you know the depth of each node. The following code retrieves the depth of each node and indents appropriately.
PrivateSub btnShowIndentation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowIndentation.Click
Me.TextBox1.Text = ""
Dim rd As XmlReader = XmlReader.Create(Application.ExecutablePath & "......Employee.xml")
In the previous section, we read only the tags available in an XML document. In this section, we will read the values and also the related tags along with them. First of all, let us concentrate on reading values. Consider the following code:
PrivateSub btnShowValues_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowValues.Click
Me.TextBox1.Text = ""
Dim rd As XmlReader = XmlReader.Create(Application.ExecutablePath & "......Employee.xml")
You can observe that I am comparing with the “text” type of SML node to retrieve the values available in XML document. You can also observe that I am using the “ReadString” method to read the value.
Finally, the following code retrieves each and every tag along with its name, text, and so forth:
PrivateSub btnShowXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowXML.Click
Me.TextBox1.Text = ""
Dim rd As XmlReader = XmlReader.Create(Application.ExecutablePath & "......Employee.xml")
In all of the previous sections, we read XML documents using XMLReader. In this section, I shall read a string which contains XML and display the node values (or text) on to the screen.
The following routine prepares the XML string using the “StringBuilder” available in the “System.Text” namespace:
If you wanted to go (parse) through the XML document tag by tag (including every start and end element of every tag), you can modify the above code as follows:
PrivateSub btnReadNodes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadNodes.Click
Me.TextBox1.Text = ""
Dim strRdr AsNew System.IO.StringReader(getPreparedXML)
XSL stands for eXtensible Stylesheet Language. It is a language used to design and apply style sheets especially for XML documents. Originally the research was started to provide style sheet technology to XML using XSL, but we finally ended up with three more divisions of XSL. So XSL now consists of three parts, namely XSLT, XPath and XSL-FO.
XSLT is a language for transforming XML documents (even today, several programmers say XSL when they really mean XSLT). XPath is a language to filter, search or sort information available in XML documents. XSL-FO is a language for formatting XML documents. In this article we mainly focus on XSLT, which stands for XSL Transformations.
XSLT can also be used to transform an XML document to another XML document (it need not be used only for HTML documents). Another beauty of XSLT is that it internally works using XPath language. We can even conclude that “The more we learn about XPath, the better XSLT we can design.” Although the sections from this point forward mainly focus on XSL, I suggest you go through the fundamentals of the XPath language for better understanding (but it's not necessary for this article).
Before transforming an XML document, we need to define the XSLT which emits HTML based on the structure of our existing XML document. The following is the XSLT needed to emit the TABLE structure of HTML:
In the above section, the XSLT which I provided works with only one record (or only one employee). If I need to work with all of the employees in the XML document, I may need to change the XSLT to the following:
From the above you can understand that I used the “for-each” structure available in XSLT. Any feedback, suggestions, bugs, errors, improvements etc., are highly appreciated at http://jagchat.spaces.live.com