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.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 17
November 21, 2006
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

A downloadable zip file is available for this article.

This article mainly focuses on reading XML documents in several ways using Visual Basic.NET 2005.

If you are new to designing XML Schemas, I suggest you to refer my following articles:

http://www.devarticles.com/c/a/XML/Designing-Your-own-XML-Schema-Learn-the-
Essentials/

http://www.devarticles.com/c/a/XML/Designing-Your-own-XML-Schema-
Constraining-with-Restrictions/

http://www.devarticles.com/c/a/XML/Designing-Your-own-XML-Schema-
Restrictions-and-User-Defined-Types/

http://www.devarticles.com/c/a/XML/Designing-Your-Own-XML-Schema-Indicators/

http://www.devarticles.com/c/a/XML/Introduction-to-Relations-in-XML-Schema/

http://www.devarticles.com/c/a/XML/OneOne-OneMany-and-ManyMany-Relations-
in-XML-Schema/

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):

<?xml version="1.0" encoding="utf-8"?>

<Employees xmlns="http://tempuri.org/Employee.xsd">

      <Employee>

            <Empno>1001</Empno>

            <Ename>Jagadish</Ename>

            <Sal>3400</Sal>

            <Deptno>20</Deptno>

      </Employee>

      <Employee>

            <Empno>1002</Empno>

            <Ename>Chatarji</Ename>

            <Sal>4500</Sal>

            <Deptno>10</Deptno>

      </Employee>

      <Employee>

            <Empno>1003</Empno>

            <Ename>Winner</Ename>

            <Sal>3700</Sal>

            <Deptno>10</Deptno>

      </Employee>

      <Employee>

            <Empno>1004</Empno>

            <Ename>Dhanam</Ename>

            <Sal>4300</Sal>

            <Deptno>20</Deptno>

      </Employee>

</Employees>

The XML Schema for the above XML document is defined as follows:

<?xml version="1.0" encoding="utf-8"?>

<xs:schema id="Employee" targetNamespace=
"http://tempuri.org/Employee.xsd" elementFormDefault=
"qualified" xmlns="http://tempuri.org/Employee.xsd" 
xmlns:mstns="http://tempuri.org/Employee.xsd" xmlns:xs=
"http://www.w3.org/2001/XMLSchema">

      <xs:element name="Employees">

            <xs:complexType>

                  <xs:sequence>

                        <xs:element name="Employee" maxOccurs="unbounded">

                              <xs:complexType>

                                    <xs:sequence>

                                          <xs:element name="Empno" type="xs:string"/>

                                          <xs:element name="Ename" type="xs:string"/>

                                          <xs:element name="Sal" type="xs:int"/>

                                          <xs:element name="Deptno" type="xs:int"/>

                                    </xs:sequence>

                              </xs:complexType>

                        </xs:element>

                  </xs:sequence>

            </xs:complexType>

      </xs:element>

</xs:schema>

Reading the tags in an XML document using XMLReader

The following is the simplest code that can read and display all tags in an XML document:

PrivateSub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click

        Me.TextBox1.Text = ""

        Dim rd As XmlReader = XmlReader.Create(Application.ExecutablePath & "......Employee.xml")

        While rd.Read

            If rd.NodeType = XmlNodeType.Element Then

                Me.TextBox1.Text &= rd.Name & ControlChars.NewLine

            End If

        End While

        rd.Close()

    End Sub

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.

    Private Sub 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")

        While rd.Read

            If rd.NodeType = XmlNodeType.Element Then

                Me.TextBox1.Text &= Space(rd.Depth * 4) & rd.Name & ControlChars.NewLine

            End If

        End While

        rd.Close()

    End Sub

When the above code gets executed, you will receive the following kind of output:

Employees Employee Empno Ename Sal Deptno Employee Empno Ename Sal Deptno . . .

Reading the values (or text) of XML tags in an XML document using XMLReader

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")

        While rd.Read

            If rd.NodeType = XmlNodeType.Text Then

                Me.TextBox1.Text &= rd.ReadString & ControlChars.NewLine

            End If

        End While

        rd.Close()

    End Sub

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")

        While rd.Read

            Select Case rd.NodeType

                Case XmlNodeType.Element

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("<{0}>", rd.Name) & ControlChars.NewLine

                Case XmlNodeType.Text

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format(rd.Value) & ControlChars.NewLine

                Case XmlNodeType.CDATA

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("<![CDATA[{0}]]>", rd.Value) & ControlChars.NewLine

                Case XmlNodeType.ProcessingInstruction

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("<?{0} {1}?>", rd.Name, rd.Value) & ControlChars.NewLine

                Case XmlNodeType.Comment

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("<!--{0}-->", rd.Value) & ControlChars.NewLine

                Case XmlNodeType.XmlDeclaration

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("<?xml version='1.0'?>") & ControlChars.NewLine

                Case XmlNodeType.Document

                Case XmlNodeType.DocumentType

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("<!DOCTYPE {0} [{1}]", rd.Name, rd.Value) & ControlChars.NewLine

                Case XmlNodeType.EntityReference

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format(rd.Name) & ControlChars.NewLine

                Case XmlNodeType.EndElement

                    Me.TextBox1.Text &= Space(rd.Depth * 4) & String.Format("</{0}>", rd.Name) & ControlChars.NewLine

            End Select

        End While

        rd.Close()

    End Sub

Reading an XML string using XMLReader

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:

PrivateFunction getPreparedXML() As String

        Dim xmlBuilder As New System.Text.StringBuilder

        With xmlBuilder

            .AppendLine("<?xml version=""1.0"" encoding=""utf-8""?>")

            .AppendLine("<Employees>")

            .AppendLine("     <Employee>")

            .AppendLine("         <Empno>1001</Empno>")

            .AppendLine("         <Ename>Jagadish</Ename>")

            .AppendLine("         <Sal>3400</Sal>")

            .AppendLine("         <Deptno>20</Deptno>")

            .AppendLine("     </Employee>")

            .AppendLine("</Employees>")

        End With

        Return xmlBuilder.ToString

    End Function

The following code works with the above routine and displays the node information back to the user:

   Private Sub btnXMLString_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXMLString.Click

        Me.TextBox1.Text = ""

        Dim strRdr As New System.IO.StringReader(getPreparedXML)

        Dim rd As XmlReader = XmlReader.Create(strRdr)

        While rd.Read

            If rd.NodeType = XmlNodeType.Text Then

                Me.TextBox1.Text &= rd.ReadString & ControlChars.NewLine

            End If

        End While

        rd.Close()

        strRdr.Close()

    End Sub

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:

  Private Sub btnReadNodes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadNodes.Click

        Me.TextBox1.Text = ""

        Dim strRdr As New System.IO.StringReader(getPreparedXML)

        Dim rd As XmlReader = XmlReader.Create(strRdr)

        rd.Read()

        rd.ReadStartElement("Employees")

        rd.ReadStartElement("Employee")

        rd.ReadStartElement("Empno")

        Me.TextBox1.Text &= rd.ReadString & ControlChars.NewLine

        rd.ReadEndElement() 'end empno

        rd.ReadStartElement("Ename")

        Me.TextBox1.Text &= rd.ReadString & ControlChars.NewLine

        rd.ReadEndElement() 'end ename

        rd.ReadStartElement("Sal")

        Me.TextBox1.Text &= rd.ReadString & ControlChars.NewLine

        rd.ReadEndElement()  'end sal

        rd.ReadStartElement("Deptno")

        Me.TextBox1.Text &= rd.ReadString & ControlChars.NewLine

        rd.ReadEndElement() 'end deptno

        rd.ReadEndElement() 'end employee

        rd.ReadEndElement() 'end employees

        rd.Close()

        strRdr.Close()

    End Sub

Transforming an XML document to HTML using XSLT with VB 2005

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:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform">

      <xsl:template match="/">

            <html>

                  <body>

                        <table width="50%" cellspacing="0" cellpadding="0" style="font-
family:verdana;font-size:X-Small
" border="1">

                              <tr bgcolor="#336699">

                                    <th align="left">

                                          <font color="White">ID</font>

                                    </th>

                                    <th align="left">

                                          <font color="White">Name</font>

                                    </th>

                                    <th align="right">

                                          <font color="White">Salary</font>

                                    </th>

                                    <th align="right">

                                          <font color="White">Deptno</font>

                                    </th>

                              </tr>

                                    <tr>

                                          <td align="left">

                                                <xsl:value-
of
 select="Employees/Employee/Empno"/>

                                          </td>

                                          <td align="left">

                                                <xsl:value-
of
 select="Employees/Employee/Ename"/>

                                          </td>

                                          <td align="right">

                                                <xsl:value-
of
 select="Employees/Employee/Sal"/>

                                          </td>

                                          <td align="right">

                                                <xsl:value-
of
 select="Employees/Employee/Deptno"/>

                                          </td>

                                    </tr>

                        </table>

                  </body>

            </html>

      </xsl:template>

</xsl:stylesheet>

The following code transforms the XML document to the above XSLT:

  Private Sub btnSampleXSLT_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnSampleXSLT.Click

        Dim docXML As New XmlDocument

        docXML.Load(Application.ExecutablePath &
"......Employee.xml")

        Dim xslt As New Xsl.XslCompiledTransform

        xslt.Load(Application.ExecutablePath &
"......Employee.xslt")

        Dim sb As New System.Text.StringBuilder

        Dim s As New IO.StringWriter(sb)

        xslt.Transform(docXML, New XmlTextWriter(s))

        s.Close()

        Me.TextBox1.Text = sb.ToString

    End Sub

Transformation using XSLT for multiple XML nodes of the same criteria

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:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform">

      <xsl:template match="/">

            <html>

                  <body>

                        <table width="50%" cellspacing="0" cellpadding="0" style="font-
family:verdana;font-size:X-Small
" border="1">

                              <tr bgcolor="#336699">

                                    <th align="left">

                                          <font color="White">ID</font>

                                    </th>

                                    <th align="left">

                                          <font color="White">Name</font>

                                    </th>

                                    <th align="right">

                                          <font color="White">Salary</font>

                                    </th>

                                    <th align="right">

                                          <font color="White">Deptno</font>

                                    </th>

                              </tr>

                              <xsl:for-
each
 select="Employees/Employee">

                                    <tr>

                                          <td align="left">

                                                <xsl:value-
of
 select="Empno"/>

                                          </td>

                                          <td align="left">

                                                <xsl:value-
of
 select="Ename"/>

                                          </td>

                                          <td align="right">

                                                <xsl:value-
of
 select="Sal"/>

                                          </td>

                                          <td align="right">

                                                <xsl:value-
of
 select="Deptno"/>

                                          </td>

                                    </tr>

                              </xsl:for-each>

                        </table>

                  </body>

            </html>

      </xsl:template>

</xsl:stylesheet>

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

blog comments powered by Disqus
VISUAL BASIC.NET ARTICLES

- Basic Form Properties and Modality in VB.NET
- Multiple Document Interfaces in Visual Basic
- Visual Basic for Beginners
- ASP.NET Image to PDF with VB.Net
- MySQL in ASP.NET: Mono using VB.NET
- AsyncFileUpload File Type and File Size Vali...
- Visual Studio: Adding Functionality and Style
- Clocks and Countdowns
- User-defined Functions using Visual Basic Ap...
- Understanding Object Binding in VBA
- Mastering the Message Box
- Testing a Windows Forms Application
- Using Visual Basic.NET Features to Code a Wi...
- Correcting Code in a Windows Forms Applicati...
- Write Readable Code and Comments for Windows...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials