How to search XML content with XmlDocument in C#

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.

string xmlFile = Server.MapPath(“BooksList.xml”);

XmlDocument doc = new XmlDocument();

doc.Load(xmlFile);

 

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

StringBuilder str = new StringBuilder();

XmlNodeList nodes = doc.GetElementsByTagName(“Title”);

foreach (XmlNode node in nodes)

{

str.Append(“Found: <b>”);

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

str.Append(node.ChildNodes[0].Value);

str.Append(“</b><br />”);

}

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.

string xmlFile = Server.MapPath(“BooksList.xml”);

XmlDocument doc = new XmlDocument();

doc.Load(xmlFile);

 

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

StringBuilder str = new StringBuilder();

XmlNodeList nodes = doc.GetElementsByTagName(“Title”);

foreach (XmlNode node in nodes)

{

str.Append(“Found: <b>”);

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

string name = node.ChildNodes[0].Value;

str.Append(name);

str.Append(“</b><br />”);

if (name == “Harry Potter and the Deathly Hallows “)

{

// Find the Author for just this book.

// First you need to get the parent node

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

XmlNode parent = node.ParentNode;

// Then you need to search down the tree.

XmlNodeList childNodes = ((XmlElement)parent).GetElementsByTagName(“Author”);

foreach (XmlNode childNode in childNodes)

{

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

str.Append(childNode.ChildNodes[0].Value);

str.Append(“<br />”);

}

}

}

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.

XmlNodeList nodes = 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 elements in the VendorCA namespace.

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