Parsing XML with the XmlDocument

The XmlDocument Class serves as a representation of an XML document, providing comprehensive capabilities for handling XML data. When using XmlDocument, the entire XML content is read into memory, allowing developers to efficiently navigate both backward and forward within the document. Additionally, it facilitates the implementation of XPath technology, enabling powerful querying functionalities for the XML document.

Upon reading the XML content into memory, XmlDocument provides a hierarchical representation of the XML structure, organizing elements and attributes in a tree-like format. This enables developers to access and manipulate the XML data in a structured manner, making it convenient to perform various operations, such as creating, modifying, or deleting elements and attributes.

XML Parser in C# and VB.Net

Read XML with XmlDocument in VB.Net

The XPath technology integration within XmlDocument further empowers developers to execute complex queries on the XML document, enabling them to extract specific data efficiently. XPath is a powerful query language for XML that allows for the selection and navigation of nodes within the XML document based on specific criteria, enhancing the flexibility and precision of data retrieval.

Using XPath with the XmlDocument class

Parse XML with XmlDocument in VB.Net

The XmlNodeList Class serves as a structured, ordered collection of nodes in XML data. When seeking specific nodes within an XML file, developers can utilize XPath expressions, a powerful query language for XML. With the XmlNode.SelectNodes method, one can easily retrieve a list of nodes that match the specified XPath string.

Using the XmlNodeList Class, developers can efficiently manage and access multiple nodes within the XML data. The class preserves the order of nodes, providing a convenient way to iterate through the collection and perform various operations on each node.

doc.DocumentElement.SelectNodes("/Store/Product");

Reading XML with the XmlDocument class

XML File content
<Store> <Product> <Product_id>1</Product_id> <Product_name>Product 1</Product_name> <Product_price>1000</Product_price> </Product> <Product> <Product_id>2</Product_id> <Product_name>Product 2</Product_name> <Product_price>2000</Product_price> </Product> <Product> <Product_id>3</Product_id> <Product_name>Product 3</Product_name> <Product_price>3000</Product_price> </Product> <Product> <Product_id>4</Product_id> <Product_name>Product 4</Product_name> <Product_price>4000</Product_price> </Product> </Store>

Click here to download Product.xml

Parse XML with XmlDocument

The XmlNodeList Class proves to be a valuable tool for organizing and manipulating a sequence of nodes within XML data. The use of XPath expressions in conjunction with the XmlNode.SelectNodes method enhances the ease and efficiency of locating nodes based on specific criteria, making it a fundamental component of XML data processing and management. The following program describes how to use the XmlDocument class to parse an XML document in C#.

C# XmlDocument example
using System; using System.Data; using System.Xml; using System.IO; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); doc.Load("d:\\product.xml"); XmlNodeList nodes = doc.DocumentElement.SelectNodes("/Store/Product"); string product_id = "", product_name = "", product_price=""; foreach (XmlNode node in nodes) { product_id = node.SelectSingleNode("Product_id").InnerText; product_name = node.SelectSingleNode("Product_name").InnerText; product_price = node.SelectSingleNode("Product_price").InnerText; MessageBox.Show(product_id + " " + product_name + " " + product_price); } } } }
VB.Net XmlDocument example VB.Net XmlDocument example

This following program describes how to use the XmlDocument class to parse an XML document in 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 doc As New XmlDocument() doc.Load("d:\product.xml") Dim nodes As XmlNodeList = doc.DocumentElement.SelectNodes("/Store/Product") Dim product_id As String = "", product_name As String = "", product_price As String = "" For Each node As XmlNode In nodes product_id = node.SelectSingleNode("Product_id").InnerText product_name = node.SelectSingleNode("Product_name").InnerText product_price = node.SelectSingleNode("Product_price").InnerText MessageBox.Show(product_id & " " & product_name & " " & product_price) Next End Sub End Class

Conclusion

The XmlDocument Class is a versatile tool for working with XML documents, offering the ability to load XML content into memory and providing efficient navigation and querying capabilities. By utilizing XmlDocument, developers gain a powerful toolset for effectively managing and processing XML data, making it a valuable choice for a wide range of XML-related tasks.