Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 4 - Reading and Transforming XML Documents usi...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Reading and Transforming XML Documents using Visual Basic 2005
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 12
    2006-11-21

    Table of Contents:
  • Reading and Transforming XML Documents using Visual Basic 2005
  • Reading the tags in an XML document using XMLReader
  • Reading the values (or text) of XML tags in an XML document using XMLReader
  • Reading an XML string using XMLReader
  • Transforming an XML document to HTML using XSLT with VB 2005
  • Transformation using XSLT for multiple XML nodes of the same criteria

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Reading and Transforming XML Documents using Visual Basic 2005 - Reading an XML string using XMLReader


    (Page 4 of 6 )

    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

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · Hello guys! this is the article which helps you to parse through XML documents using...
     

    VISUAL BASIC.NET ARTICLES

    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...
    - More on Controlling Windows Fax Services Usi...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway