How to open and read XML file in VB.NET

In the previous program, we generated an XML file called "products.xml." Now, we will focus on reading the contents of this XML file and extracting the information enclosed within specific XML tags. There are multiple approaches available for reading an XML file, and the chosen method depends on the specific requirements of the task at hand.

XmlDataDocument class

In this program, we will be reading the XML file in a node-wise manner. To accomplish this, we will utilize the XmlDataDocument class, which provides functionality for parsing and navigating XML data. By employing this class, we can efficiently traverse through the XML file and extract the desired data.

In the program, we target the "<Product>" node and its child nodes to extract the relevant information stored within them. The XmlDataDocument class enables us to access these specific nodes and retrieve the data they contain.

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 highly efficient and memory-friendly alternative for reading XML files in VB.NET. It offers a lower-level abstraction over the structure of an XML file, allowing for streamlined processing and reduced memory consumption.

The XmlReader class operates by sequentially parsing the XML string, enabling you to navigate through the XML elements one at a time. This approach ensures a minimal memory footprint since the entire XML document is not loaded into memory at once. Instead, XmlReader reads the XML content in a forward-only manner, making it suitable for scenarios where memory usage is a concern or when dealing with large XML files.

The XmlReader class's design focuses on efficiency and speed, making it an ideal choice when performance optimization is a priority. It is particularly useful when you only need to extract specific data or perform selective operations on the XML file.

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

The XmlDocument class in VB.NET offers a comprehensive approach for reading and manipulating XML files. It provides the capability to load the complete XML content into memory, enabling flexible navigation and querying of the XML document using the XPath technology.

When utilizing XmlDocument, the entire XML content is loaded into memory, allowing for efficient traversal and manipulation of the XML structure. This in-memory representation enables easy navigation and exploration of the XML document by providing methods and properties to access specific elements, attributes, and text nodes.

Click here to download the input file : product.xml

Full Source VB.NET
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

Conclusion

XmlReader provides a fast and memory-efficient approach for reading XML files in VB.NET. Its forward-only, sequential processing capability allows for efficient traversal of XML elements and retrieval of their values. When dealing with large XML files or performance-sensitive scenarios, XmlReader offers superior performance and reduced memory consumption compared to higher-level XML parsing approaches.