- 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:
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????
v nice
its really very helpful
how do we retrieve the attributes value in a string variable
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?
Nups it wasn't a problem :/ i did some silly mistake.. :|
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?
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.
Post a Comment