This is a demonstration of a web based client for Google Web Services. This is my first attempt at ASP.NET technology.
I have tried to make it fully customizable and open ended as far as possible.
Key customization features are:
- The search result display is controlled by an XSL file (search.xsl). Change the look and feel and have fun customizing the result set the way you want it.
- The application reads from a cfg.xml file on start up. It contains all the search query parameters and also some application specific settings. For example, the key required to access the Google search database. You can find this file in the attached zip fie.
The app looks for cfg.xml in the current working directory which would be c:\winnt\system32 by default. Copy this file to that location.
Advantage of having this is search query terms can be changed as and when requirements change, and same holds good for the results display.
This application allows a user to go through the first 1000 results since Google places that restriction.
What do you need to run the demo project?
- IIS 5.0 on Windows 2000/.NET Framework version 1.0.
- Required: Register with Google and get your personal license key. This is a must.
- Required: Open the cfg.xml included in the source and fill in the values for
<XslPath/>. Explanation of these keys in a moment...
What the Application does
The application sends a SOAP message to the Google Server which in turn returns a Google proprietary data structure. The application then parses this data structure and creates an XML file. (Let's call it results.xml. Note: It could be any name you choose as long as it's in the cfg.xml.) Here is a small portion of it:
<SearchResults attributes1=value1... >
<title>Welcome to SAWStudio.com!</title>
The application then transforms this XML file using the custom stylesheet (search.xsl) and writes the results of the transformation to the client browser.
OK, so that's what the
<XslPath/> keys mean.
<ResultsPath/> is the fully qualified path to the file where you want the app to dump the XML.
<XslPath /> is the fully qualified path to the file where the stylesheet (search.xsl) is stored.
Key areas of the source code:
Search class in WebForm1.aspx.cs
Search class defined in the code-behind of WebForm1.aspx, which is the only webform used in the entire project, has the core function
dumpResultsasXml() which does exactly as named.
System.XmlTextWriter does the writing.
Transform() function does the transformation to HTML. I have used a
MemoryStream object to read the transformation results, and subsequently, a
StreamReader to read that memory and convert it to string data type.
WebForm1.getdata is where the action actually happens. This can be called from within the WebForm1.aspx like this:
<% Response.Write(getdata()) %>
Points of interest:
There is only a single instance of the Google Search Service object which is initialized in
Although the "Next" and "Previous" links could have been generated by the search.xsl file, there is no way for us to control the starting index value in the XSL other than incrementing it by
x like this:
<xsl:variable name="startindex" select="@currentIndex+10"/> ... and so on.
But the XSL would be blissfully unaware of what page we are on. The following code shows how it's been implemented:
start = Request.Params["start"];
results = getdata(query,Convert.ToInt32(start));
results = getdata(query,0);
Tracking is done using the querystring parameter
start to which
x is added or subtracted, to generate the URL for the "Next " and "Previous" links. In this case,
x =10. It's a good idea to have this as one of the settings in the cfg.xml, so it would be more generic. Well, next release maybe :)
Here is an example of how the Next link is generated, where start is the value of the querystring parameter
nTotalCount is the total number of results.
if((nTotalCount - nNext) > 10)
Well, I hope I have covered the key areas. Like any other program, I am certain there would be bugs and mistakes I may have made. Looking forward to your feedback so I may incorporate enhancements and bug fixes if any.