Wednesday, November 5, 2008

How to bind XQuery result string to a DataSet

In my previous blog I explained how to use XQuery in .NET platform. You can see it from here. In this blog I will explain how to display the result of a XQuery by using DataGrid in .NET.

As a beginning we have a string value which has a format like XML and holds the result of our XQuery like:

Example of string value:


Our main concern is to use DataSet.ReadXML() method but the problem is this method does not take String value as an input. It takes one of the File, Stream, XMLReader or TextReader typed objects. So how can we use it ?
  • First we can write our String value to a new file which we can create on fly then give it to method.

  • We can create either of XMLReader and TextReader objects and assign our file to these objects the put the output to out method.

I think these solutions are to complicated and there are too much steps on them. While I was looking for any neat solution, I found using Stream isn`t a bad idea to be used here. Also it was my only choice left.

So let`s start;

  1. First we need to create a MemoryStream object, because ReadXML() takes Stream typed objects.

  2. Constructor of the MemoryStream class is accepting byte[] that we can easily cast our String value.

This is how we cast our String value:
System.Text.ASCIIEncoding.ASCII.GetBytes(result)

As a result we are getting our Stream value like:

MemoryStream mstream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(result));

After we filled up our Stream object, we need to put it into ReadXML() method of our DataSet;

DataSet ds = new DataSet();
ds.ReadXml(mstream);

Now we can use this DataSet where ever we want, for example we can easily bind it to a DataGrid.

If you want to learn how to bind this DataSet to a DataGrid, look for my post about it..

Cya..

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...