Net-informations.com
SiteMap  | About    

How to open and read XML file in VB.NET

XML is a self describing language and it gives the data as well as the rules to extract what the data it contains. Reading an XML file means that we are reading the information embedded in XML tags in an XML file.

VB.Net XML Parser

how to load xml in VB.Net

In the previous program we create an XML file and named it as products.xml. The following program read that file and extract the contents inside the XML tag. We can read an XML file in several ways depends on our requirement. This program read the content in Node wise . Here we are using XmlDataDocument class to read the XML file . In this program it search the Node < Product > and its child Nodes and extract the data in child nodes.

Click here to download the input file : product.xml

How to read XML from a file in VB.Net

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

Reading Xml with XmlReader in VB.Net

VB.Net XML Handling

XmlReader is a faster and less memory consuming alternative. It provides a lower-level abstraction over the XML file structure. The XmlReader class allows you run through the XML string one element at a time, while allowing you to look at the value, and then moves on to the next XML element.

VB.Net XML Handling

Dim readXML As XmlReader = XmlReader.Create(New StringReader(xmlNode)) While readXML.Read() Select Case readXML.NodeType Case XmlNodeType.Element ListBox1.Items.Add("<" + readXML.Name & ">") Exit Select Case XmlNodeType.Text ListBox1.Items.Add(readXML.Value) Exit Select Case XmlNodeType.EndElement ListBox1.Items.Add("") Exit Select End Select End While

Full Source : Reading Xml with XmlReader

Reading XML with XmlTextReader in VB.Net

XmlTextReader Class provides forward-only, read-only access to a stream of XML data.

Dim reader As New XmlTextReader("d:\product.xml") While reader.Read() Select Case reader.NodeType Case XmlNodeType.Element listBox1.Items.Add("<" + reader.Name & ">") Exit Select Case XmlNodeType.Text listBox1.Items.Add(reader.Value) Exit Select Case XmlNodeType.EndElement listBox1.Items.Add("") Exit Select End Select End While

Full Source : Reading Xml with XmlReader

Reading XML with XmlDocument in VB.Net

Introduction to XML with vb.net

XmlDocument reads the entire XML content into memory and then allow you to navigate back and forward in it or even query the XML document using the XPath technology.

Click here to download the input file : product.xml




Imports System.Xml
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xmlDoc As New XmlDocument()
        xmlDoc.Load("d:\product.xml")
        Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("/Table/Product")
        Dim pID As String = "", pName As String = "", pPrice As String = ""
        For Each node As XmlNode In nodes
            pID = node.SelectSingleNode("Product_id").InnerText
            pName = node.SelectSingleNode("Product_name").InnerText
            pPrice = node.SelectSingleNode("Product_price").InnerText
            MessageBox.Show(pID & " " & pName & " " & pPrice)
        Next
    End Sub
End Class

 VB.NET XML - Related Contents