Tuesday, November 4, 2008

Using XQuery in C#

Last week I was working on fetching data over XML to be used in ASP.NET project. I had been studying XQuery for a while and I realized it is easy and neat language to query on XML document. There also some other technologies that are able to do same things but I chose to use XQuery because it`s logic is very similar to common programming languages. While I`m trying to find how to use XQuery in .NET platform, I couldn't`t find any built in library in .NET framework but I found a library that was created by third party.
  • You can download the class library from here.
  • After downloading the file just move .dll file to your project`s bin folder.
  • As last step you need to add this .dll file as reference to your project.

After doing all these steps you will be able to use this library in your project. Also you should attach it to your code like:
using Microsoft.Xml.XQuery;

First, we need to create XQueryNavigatorCollection instance:

XQueryNavigatorCollection col = new XQueryNavigatorCollection();

Then we need to link the xml data file to our instance, AddNavigator method`s first input will be the path of the data file and second one is the alias of that file. This alias will be useful while writing our XQuery to point out our data file.

col.AddNavigator(Server.MapPath("Data.xml"), "doc");

We are writing our XQuery and assign it to a string value, as you can see I used "doc" alias that points Data.xml file:
string query = "for $row in document(\"doc\")/Import/Row " +
"return " +
"$row";

After assigning our query to a string we give this string to XQueryExpression class`s constructor, by doing this step our query will be ready to be executed:

XQueryExpression xepr = new XQueryExpression(query);

To execute our query and get the output from it we are just writing:

string result = xepr.Execute(col).ToXml();

and getting the result to string result variable. By getting the result you can do whatever you want with it. You can just display it as it is or by assigning it to a DataSet you can give to it more usable scope in your project.

After a while I will post how can you assign it to a DataSet...

9 comments:

Nikhil Kumar Tiwari said...

This article is really very helpfull.
You have cleared everything very nicely.
I want to know abt how to bind that result to dataset.
Could u pls help me????

Muhammad Muneer said...

v nice
its really very helpful

Rishabh said...

how do we retrieve the attributes value in a string variable

Unknown said...

yeah it's really really helpful article.. U've explained each and every point. So that one can easily start working on Xquery..
But can u please confirm me that can i make it on console app also? i made the application a/c to ur explanation and intellisence was also appearing after importing the xquery namespace but when i've built it.. it started giving error on the namespace.. i.e. Microsoft.Xml.XQuery? any help?

Unknown said...

Nups it wasn't a problem :/ i did some silly mistake.. :|

esztersz said...

I would be glad if you mentioned in your post that delete is not implemented (it took me a while till I noticed). Also, what about insert, replace and rename?

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

Thank you for your useful explanation.
I just wished to add that there should be more precise XQuery, in aim to be executed properly.
1. string query = "OPEN ROOT TAG { FOR ..... return { OPEN ITEM TAG values CLOSED ITEM TAG }... } CLOSED ROOT TAG".

2. there should be "//Items/Item" in for $f in document(\"doc\")//Items/Item" + " return "

I hope this comment will be displayed properly, since previous two comments were displayed incompletely.