Click here to Skip to main content
15,868,051 members
Articles / Programming Languages / XML
Article

Searching multiple XML files

Rate me:
Please Sign up or sign in to vote.
4.75/5 (20 votes)
28 Sep 20053 min read 137.7K   4.4K   42   24
A simple utility to quickly find nodes and attributes inside multiple XML files.

Introduction

Did you ever need to find specific text inside an XML file? Or did you ever need to identify, in a set of XML files, those containing a specific text? If yes, probably you used some kind of "multiple file search" utility, or simply the operating system "Search" feature, able to locate files containing a given text string. This is obviously a suitable solution for many cases. But... what about looking for specific text appearing in specific locations of the XML structure? For example: if you need to identify XML files containing a string inside a particular attribute value and *not* somewhere else, a generic text search tool becomes useless.

The simple utility presented here, named "XML Search", addresses this problem, implementing a multiple XML file search mechanism that takes care of the XML file structure.

How the utility works

The utility I wrote is very simple: given an "input" folder, it looks for XML files in that folder (and eventually subfolders), loading them - one by one - into an XmlDocument object, and looking for specific elements that match the search criteria you specified. The search outcome is a list of the successfully matching files, with the matching XML fragment highlighted.

XML Search user interface

Instead of an "input" folder, you can also specify a single file: in this case, the search task is limited to the given file content.

The search criteria are basically identified by an XPath expression you specify in the "Node selection condition" textbox: this expression will be used to select (in each XML file) the XML nodes where the tool will look for the text string you typed in the "Find what" textbox.

Some option buttons allow you to specify if the search has to be done:

  • on the InnerText property of the selected nodes
  • on the InnerXML property of the selected nodes
  • on the OuterXML property of the selected nodes
  • on a specific attribute value of the selected nodes

You can also use the XML Search tool just to locate XML files containing specific nodes or attributes (when their value doesn't matter): to achieve this, you'll use the "Just check for node existence" option.

In the XPath search condition you may want to omit the root element of the XML structure; in this case you will check the "Start from DocumentElement" checkbox.

If you need a case-sensitive search, you will check the "Match case" checkbox (default search is case-insensitive on the "Find what" value, but be aware that the XPath expression is always case-sensitive).

If you need to exactly look for a given value, you will check the "Match whole text" checkbox; otherwise, the "Find what" value will be searched inside the XML fragment selected by your search criteria.

When the search ends, a list of matching files will be displayed:

  • by clicking on a specific item in the list, the bottom panel will show the matching XML fragment found in that file (normally, the matching XML fragment is extracted from the OuterXML property of a matching node).
  • by right-clicking on a specific item in the list, you'll get a context menu allowing you to copy on the Windows Clipboard: the entire content of the selected file, the filename of the selected file, the full path of the selected file (last two options are also active when multiple items are selected in the file list).

Points of interest

The XML Search utility is very simple: it essentially puts together a recursive folder scanning with an XML DOM computation based on the SelectNodes method. The power of .NET classes makes the rest.

For the XML fragments displaying in the bottom panel, I used the very good "XML TreeView Control" found on CodeProject: I have to thank Thomas Siepe for it. I invite you to read his article about the control.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Technical Lead
Italy Italy
I was born in 1970.

My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.

I graduated in Electronic Engineering and earned the following Microsoft certifications:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

I worked in IT as a developer, a teacher, a consultant, a technical writer, a technical leader.
IT knowledge applied to real life is my primary interest and focus.

Comments and Discussions

 
Questionexecute XML Search Pin
Member 150112223-Dec-20 8:45
Member 150112223-Dec-20 8:45 
QuestionHow do I find the following attribute in multiple files with you App Pin
Member 122448334-Jan-16 23:38
Member 122448334-Jan-16 23:38 
AnswerRe: How do I find the following attribute in multiple files with you App Pin
Alberto Venditti5-Jan-16 11:18
Alberto Venditti5-Jan-16 11:18 
Questionmultiple results per file? Pin
eddieb2124-Oct-11 1:15
eddieb2124-Oct-11 1:15 
AnswerRe: multiple results per file? Pin
Alberto Venditti4-Dec-11 6:39
Alberto Venditti4-Dec-11 6:39 
QuestionNode Section condition Pin
Raider3bravo17-Aug-11 10:16
Raider3bravo17-Aug-11 10:16 
AnswerRe: Node Section condition Pin
Alberto Venditti18-Aug-11 4:06
Alberto Venditti18-Aug-11 4:06 
QuestionMultithreading? Pin
HoyaSaxa9317-Aug-09 14:05
HoyaSaxa9317-Aug-09 14:05 
AnswerRe: Multithreading? Pin
Alberto Venditti30-Aug-09 22:42
Alberto Venditti30-Aug-09 22:42 
GeneralRe: Multithreading? Pin
HoyaSaxa9331-Aug-09 4:41
HoyaSaxa9331-Aug-09 4:41 
GeneralNice job. Just what I was looking for. Pin
gary caden16-Apr-09 8:15
gary caden16-Apr-09 8:15 
GeneralRe: Nice job. Just what I was looking for. Pin
JMWTech11-Dec-09 4:51
JMWTech11-Dec-09 4:51 
GeneralRe: Nice job. Just what I was looking for. Pin
Alberto Venditti10-Jan-10 3:25
Alberto Venditti10-Jan-10 3:25 
Generalhelp with a problem Pin
Catherine Bell26-Mar-09 3:56
Catherine Bell26-Mar-09 3:56 
GeneralRe: help with a problem Pin
Alberto Venditti27-Mar-09 2:25
Alberto Venditti27-Mar-09 2:25 
Generalvery good, clean and effective and easy to follow Pin
androoo26-Sep-06 11:00
androoo26-Sep-06 11:00 
GeneralInner/Outer XML and text searches Pin
VBProEd23-Mar-06 11:41
VBProEd23-Mar-06 11:41 
GeneralRe: Inner/Outer XML and text searches Pin
Alberto Venditti23-Mar-06 20:46
Alberto Venditti23-Mar-06 20:46 
GeneralRe: Inner/Outer XML and text searches Pin
VBProEd25-Mar-06 9:44
VBProEd25-Mar-06 9:44 
QuestionQuestions about Microsoft Exams you passed Pin
egc10128-Oct-05 9:31
egc10128-Oct-05 9:31 
AnswerRe: Questions about Microsoft Exams you passed Pin
Alberto Venditti28-Oct-05 21:52
Alberto Venditti28-Oct-05 21:52 
QuestionHow about a code download? Pin
seanwright29-Sep-05 5:49
seanwright29-Sep-05 5:49 
AnswerRe: How about a code download? Pin
Alberto Venditti29-Sep-05 8:20
Alberto Venditti29-Sep-05 8:20 
GeneralRe: How about a code download? Pin
Hypnotron30-Jul-06 7:34
Hypnotron30-Jul-06 7:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.