To read the article online, visit

Querying XML Data with XQuery, Part 2

By Scott Mitchell

  • Read Part 1

  • 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 In order to start using XQuery expressions in your ASP.NET Web applications, you must first download the xquery.msi file at 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 Microsoft.Xml.XQuery.dll. To use XQuery expressions in an ASP.NET Web application, simply copy this file to the /bin directory of your ASP.NET application. Next, you will want to import the Microsoft.Xml.XQuery namespace in your code-behind class. Once you have performed these steps, you can start using XQuery expressions! Down
    Since at least July 27, 2003, 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 xquery.msi file from the 4Guys server here.

    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 create an XQueryNavigatorCollection instance and add to it an XML file:

    Dim col as New XQueryNavigatorCollection()
    col.AddNavigator(Server.MapPath(filename), alias)

    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:

    for $x in document(foo) return $x/bar

    the alias foo is being used to reference the XML document. So, in your source code, you'd want to use:

    Dim col as New XQueryNavigatorCollection()
    col.AddNavigator(Server.MapPath(filename), "foo")

    To actually perform the XQuery expression, we need to create an instance of the XQueryExpression class, passing in the XQuery expression we wish to execute in to the object's constructor. Following this, we call the XQueryExpression's object's 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 XQueryExpression object, running the query, and getting back the raw XML:

    Dim query as String
    query = "for $x in document(""foo"")//bar " & _
            "where $x/something = 4 " & _
            "return $x/somethingElse"
    Dim expr as New XQueryExpression(query)
    Dim rawXML as String = (expr.Execute(col)).ToXml()
    [View a Live Demo!]

    That's all there is to it! The XQuery engine download from, 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 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:

    Happy Programming!

  • By Scott Mitchell

  • Article Information
    Article Title: ASP.NET.Querying XML Data with XQuery, Part 2
    Article Author: Scott Mitchell
    Published Date: July 16, 2003
    Article URL:

    Copyright 2021 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers