Querying XML Data with XQuery, Part 2By Scott Mitchell
In Part 1 we examined what XQuery is and saw some example XQuery syntax. In particular, we examined the FLWR expression. In this final part we'll look at Microsoft's .NET XQuery engine, and how to start using XQuery in ASP.NET today.
XQuery for .NET
While the XQuery standard is not yet 100% complete, there are many working implementations for a variety of platforms. Microsoft provides a free XQuery engine for .NET, which is available at XQueryServices.com. In order to start using XQuery expressions in your ASP.NET Web applications, you must first download the
xquery.msifile at XQueryServices.com and start the installation process.
The installation process will create a new directory,
C:\Program Files\XQuery Demo, which
contains, among other files and subdirectories, a file named
To use XQuery expressions in an ASP.NET Web application, simply copy this file to the
directory of your ASP.NET application. Next, you will want to import the
namespace in your code-behind class. Once you have performed these steps, you can start using XQuery
Since at least July 27, 2003, XQueryServices.com has been down. (See this blog entry.)
In any event, the word from Microsoft is that this site is having some "issues" and should be back up
soon. In the meantime, you can download the contents of the |
Recall that all XQuery expressions are performed over some XML document. The first step, then, in programmatically
using XQuery expressions is to specify the XML document that will be involved in the search. To do
so, we must create an
XQueryNavigatorCollection instance, and add to it the XML documents
that we plan on using. (Note that we can perform XQuery expressions over more than one XML document,
such as in performing a join over two XML documents.) The following code demonstrates how to
XQueryNavigatorCollection instance and add to it an XML file:
Alias is a string alias that the XML document will be referred to in the XQuery expression. For example, if you had an XQuery expression like:
foo is being used to reference the XML document. So, in your source code,
you'd want to use:
To actually perform the XQuery expression, we need to create an instance of the
class, passing in the XQuery expression we wish to execute in to the object's constructor. Following this,
we call the
Execute() method, passing in the
XQueryNavigatorCollection instance we created earlier. This will return an
XQueryNavigator instance. We can call this object's
ToXml() method to get
the raw XML back. The following code demonstrates creating and
running the query, and getting back the raw XML:
That's all there is to it! The XQuery engine download from XQueryServices.com, includes sample code showing executing an XQuery expression in both C# and VB.NET. Furthermore, it contains a plethora of XQuery expression examples.
In this article we examined the basics of XQuery and looked at some sample XQuery syntax and example expressions. While the XQuery expressions examined in this article were fairly simple, do not let this fool you into thinking that XQuery can only perform simple queries.
In addition to examining the basic syntax of XQuery, we also looked at how to start using XQuery with .NET. Microsoft provides a free XQuery engine at XQueryServices.com that you can use in your .NET desktop applications or in ASP.NET Web applications. Now is as good a time as any to start learning XQuery, as XQuery is bound to become more prominent as XML data stores continue their meteoric rise. Furthermore, the next version of SQL Server will have inherent XQuery support.
To assist with learning XQuery, you might find the following articles helpful:
- XQuery, the Query Language of the Future
- XQuery: An XML Query Language (PDF)
- Five Practical XQuery Applications
- What is XQuery?
- An Introduction to XQuery