Wednesday, May 12, 2004

Searching Indexing Services with ASP.NET and C# Part II

Last friday I blogged about using Ixsso and ASP.NET to use as a site search. Well,I had to go and play with it a little more. The one thing I didn't like about the example that was given on friday was that it used a datagrid. Now don't get me wrong, I love datagrids in ASP.NET. However I just don't think of search results as tabular data. It's much more in a paragraph form. Look at google, you'll see a header/title, an abstract or paragraph and the URL. That is what people are use to seeing went they do site searches. Seeing the ranking is cool for us geeks but for most users, they want the search to be smart enough that, it is displayed in the order it was ranked in. So yesterday I had the idea that I should just serialize the results that I got from Indexing Services and use XSL to display the data. This is a great way to change the look of the results very quickly. However there was a side benefit that I didn't think of, it's fast. Now true I'm not searching an index of 6 billion pages like google, but I was amazed how quick it was. So how about some code now. This piece of code starts right before we were binding the results to the datagrid in friday's example.

da.Fill(ds, Q.CreateRecordset("nonsequential"), "IndexServerResults");

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(ds.GetXml());

XslTransform xslDoc = new XslTransform();
xslDoc.Load(utilities.getPath("xsl/search.xsl"));
xslDoc.Transform(xmlDoc, null, sw, null);
xmltest = sw.ToString();

So what we did was serialize the dataset by calling ds.GetXml(). From there we converted the string that ds.GetXml() generates by creating an new XmlDocument and loading the xml string into it by calling xmlDoc.LoadXml(ds.GetXml()). After that we created an instance of XslTransform, loaded the xsl file in to memory, and transform the xml to html, and returned it as string to be used in our aspx code. Pretty cool, huh?

No comments: