How to search XML content with XmlDocument in VB.NET

You can perform a search with the XmlDocument by using  XmlDocument.GetElementsByTagName() method, which  searches an entire document tree for elements that have a specific name and returns an XmlNodeList that contains all the matches as XmlNode objects.

For example, the following code retrieves the title of each Book in the document:

 

‘ Load the XML file.

Dim XmlFile As String = Server.MapPath(“BooksList.xml”)

Dim Doc As New XmlDocument()

 

Doc.Load(XmlFile)

 

‘ Find all the <Title> elements anywhere in the document.

Dim Str As New StringBuilder()

Dim Nodes As XmlNodeList = Doc.GetElementsByTagName(“Title”)

For Each Node As XmlNode In Nodes

Str.Append(“Found: <b>”)

‘ Show the text contained in this <Title> element.

Str.Append(Node.ChildNodes(0).Value)

Str.Append(“</b><br />”)

Next

 

LblXml.Text = Str.ToString()

 

You can also search portions of an XML document by using the method XmlElement.GetElementsByTagName() on a specific element. In this case, the XmlDocument searches all the descendant nodes looking for a match. To use this method, first retrieve an XmlNode that corresponds to an element and then cast this object to an XmlElement. The following example demonstrates how to use this technique to find the stars of a specific book:

 

‘ Load the XML file.

Dim XmlFile As String = Server.MapPath(“BooksList.xml”)

Dim Doc As New XmlDocument()

Doc.Load(XmlFile)

 

‘ Find all the <Title> elements anywhere in the document.

Dim Str As New StringBuilder()

Dim Nodes As XmlNodeList = Ddoc.GetElementsByTagName(“Title”)

For Each Node As XmlNode In Nodes

Str.Append(“Found: <b>”)

‘ Show the text contained in this <Title> element.

Dim Name As String = Node.ChildNodes(0).Value

Str.Append(name)

Str.Append(“</b><br />”)

If Name = “Harry Potter and the Deathly Hallows ” Then

‘ Find the Author for just this book.

‘ First you need to get the parent node

‘ (which is the <Book> element for the Book).

Dim Parent As XmlNode = Node.ParentNode

‘ Then you need to search down the tree.

Dim ChildNodes As XmlNodeList = DirectCast(parent, XmlElement).GetElementsByTagName(“Author”)

For Each ChildNode As XmlNode In ChildNodes

Str.Append(“&nbsp;&nbsp; Found Author: “)

Str.Append(childNode.ChildNodes(0).Value)

Str.Append(“<br />”)

Next

End If

Next

LblXml.Text = Str.ToString()

 

When XML document is more sophisticated it includes a namespace and may even have several of them. In this case you can use the overload of the method XmlDocument.GetElementsByTagName(), which requires a namespace name as a string argument, as shown here:

 

‘ Retrieve all <vendor> elements in the VendorCA namespace.

Dim nodes As XmlNodeList = doc.GetElementsByTagName(“vendor”, “http://mycompany/VendorCA”)

 

You can use asterisk (*) for the element name if you want to match all tags in the specified namespace:

 

‘ Retrieve all <vendor> elements in the VendorCA namespace.

Dim nodes As XmlNodeList = doc.GetElementsByTagName(“*”, “http://mycompany/VendorCA”)