作者:范维肖
The Query Method

Whenever the user performs a search using our Research Service the Query method is called.  The method uses the query packet to determine the action that is required and then returns a response.

The Query method must accept a single string parameter called queryXml.

Within the Query method, it is our role as a developer to carry out three essential tasks.  The first is to parse the incoming request to determine the request that has come from the calling application.  The second is to take that request and carry out a certain action (perhaps fetching some data from a database) and the third action is to prepare the XML response to be sent as the response. 

[WebMethod]
public string Query(string queryXml){

这和前面讲Registration Method的几乎完全一样。

This method, like the Registration method, also accepts a string and returns a string.  The strings are again valid XML.  This time we parse the query to find out what kind of search the user is carrying out.

这个方法,像Registration方法一样,也是接收一个字符串然后返回一个字符串,同样,字符串也是有效的XML串。这一次我们分析这个查询来找出用户正在执行的查询是什么种类的。

ParseQuery(queryXml);

In our example we parse the query and extract the commands into a hashtable cmd.  Once we've determined the commands that the user wants to carry out, we render the output for the Research Pane using our ResearchPaneRenderer class:

在我们的例子里,我们分析了这个查询并且把commands提取出来放到哈系表命令中,一旦我们确定了用户想要执行的命令,我们就用找个类:

string xmlOutput = null;
MarketingService ms 
= new MarketingService();
ResearchPaneRenderer rpr 
= new ResearchPaneRenderer();
switch(cmd)

The Research pane renderer can include simple text (with some font attributes applied) hyperlinks (to send a new query, or open a browser window), simple form elements and other items.

查询面板可以包括简单的文字(可以包含字体属性的),链接(发送一个新的查询或开启一个浏览器窗口)或form的元素(内容)

Essentially we can write any code within our Query method, to search data from a database, to perform XSL Transforms or anything we want, provided that the output confirms to the correct format of a ResponsePacket.

我们可以在我们的Query方法里写一些代码,能完成从数据库里查询数据的代码或执行XSL传输,或者是其它我们想要做的,记得输出是要遵循正确的格式哦。

<ResponsePacket xmlns="urn:Microsoft.Search.Response" revision="" build="" providerRevision="">
   
<Response domain="">
       
<QueryId/>
       
<Copyright/>
       
<OriginatorContext>
          
<Any/>
       
</OriginatorContext>
       
<RequeryContext>
          
<Any/>
       
</RequeryContext>
       
<Range id="">
          
<ResultType/>
          
<StartAt/>
          
<Count/>
          
<TotalAvailable/>
          
<Results>
              
<Any/> *
          
</Results> 
          
<Any/>
       
</Range>
       
<Status/>
       
<DebugErrorMessage/>
       
<Any/>
   
</Response>
   
<Any/>
</ResponsePacket>

To ensure that we conform to this format, we use XSLT style sheets.  In the solution explorer open the ResearchCustomers.XSLT file in the Data Folder.

我们使用XSLT样式表来保证我们遵循了这种格式。在解决方案的视图窗口里打开Data目录下ResearchCustomers.XSLT文件。

And finally the response is sent to the calling application as a well-formed and valid XML file.

最后,Response以格式良好的的有效的XML文件的形式发送回所请求的app。

posted on 2005-02-07 20:12  维生素C.NET  阅读(923)  评论(0)    收藏  举报