XQuery Demo

This demo illustrates how to execute an XQuery expression and retrieve the raw XML result.


Below you will find the results to the XQuery expression:

	for $d in document("foo")//folder 
    where $d/@name="Quake" or count($d/file) >= 2
    return 
              {string($d/@name)}
              {$d/file}
           
    

The XML document the XQuery expression is operating on is FileSystem.xml.

<folder><name>Quake</name><files><file>quake.exe</file><file>README.txt</file><file>EULA.txt</file></files></folder><folder><name>SavedGames</name><files><file>game1.sav</file><file>game2.sav</file></files></folder><folder><name>Backup</name><files><file>2003-06-01.bak</file><file>2003-06-07.bak</file></files></folder>


Source Code
<%@ Import Namespace="Microsoft.Xml.XQuery" %>
<script language="VB" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
     Dim col as New XQueryNavigatorCollection()
     col.AddNavigator(Server.MapPath("FileSystem.xml"), "foo")

     Dim query as String
     query = "for $d in document(""foo"")//folder " & _
             "where $d/@name=""Quake"" or count($d/file) >= 2 " & _
             "return <folder>" & _
             "          <name>{string($d/@name)}</name>" & _
             "          <files>{$d/file}</files>" & _
             "       </folder>"

     Dim expr as New XQueryExpression(query)
     lblXML.Text = (expr.Execute(col)).ToXml()
  End Sub
</script>

<xmp>
<asp:Literal runat="server" id="lblXML" />
</xmp>


[Return to the article...]