How to Import XLM Into Visual Basic

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Import XLM Into Visual Basic
Read XML files in VB. (Jupiterimages/Photos.com/Getty Images)

XML files are widely used and it can be handy to know how to import XML data into your Visual Basic application. To accomplish this task you need to use the "System.Xml" assembly. This assembly contains the "XmlTextReader" class, which you can use to read Externsible Markup Language (XML) from a file. The reader provides direct parsing and tokenizing of XML. The data read is stored in memory that you can access and use in your VB program.

Skill level:
Moderate

Other People Are Reading

Instructions

  1. 1

    Launch Microsoft Visual Basic Express, click "New Project" from the left pane of your computer screen and click "Console Application." Click "OK" to create a new console project.

  2. 2

    Add the following line of code as the first line of your code module:

    Imports System.Xml

  3. 3

    Type the following code to define the XML reader and the XML file to read:

    Dim reader As XmlTextReader = New XmlTextReader("C:\xmlFile.xml")

  4. 4

    Copy and paste the following code to read the XML file and display its contents through the Console window:

    Do While (reader.Read())

            Select Case reader.NodeType
    
                Case XmlNodeType.Element
    
                    Console.Write("<" + reader.Name)
    
                    If reader.HasAttributes Then
    
                        While reader.MoveToNextAttribute()
    
                            Console.Write(" {0}='{1}'", reader.Name, reader.Value)
    
                        End While
    
                    End If
    
                    Console.WriteLine(">")
    
                Case XmlNodeType.Text
    
                    Console.WriteLine(reader.Value)
    
                Case XmlNodeType.EndElement
    
                    Console.Write("</" + reader.Name)
    
                    Console.WriteLine(">")
    
            End Select
    
        Loop
    
        Console.ReadLine()
    
  5. 5

    Press "F5" to run the project and display the XML file contents.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.