Programming Tutorials

How to open and read an XML file in VB.net

By: Issac in VB.net Tutorials on 2009-02-21  

Since XML is a self describing language it specifies data and rules through which we can extract what data it contains, Reading an XML file means that we are reading the information embedded in XML tags in an XML file.

In the previous article we created an XML file and named it as products.xml. This article speaks about reading that file and extracting the contents inside the XML tag. We can read an XML file in several ways depending upon our requirement. This source code reads the content in Node wise. Here we are using XmlDataDocument class to read the XML file. In this program it searches the Node < Product > and its child Nodes and extracts the data in child nodes.

Imports System.Xml
Imports System.IO
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xmldoc As New XmlDataDocument()
        Dim xmlnode As XmlNodeList
        Dim i As Integer
        Dim str As String
        Dim fs As New FileStream("products.xml", FileMode.Open, FileAccess.Read)
        xmldoc.Load(fs)
        xmlnode = xmldoc.GetElementsByTagName("Product")
        For i = 0 To xmlnode.Count - 1
            xmlnode(i).ChildNodes.Item(0).InnerText.Trim()
            str = xmlnode(i).ChildNodes.Item(0).InnerText.Trim() & " | " & xmlnode(i).ChildNodes.Item(1).InnerText.Trim() & " | " & xmlnode(i).ChildNodes.Item(2).InnerText.Trim()
            MsgBox(str)
        Next
    End Sub
End Class





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)