步骤 2:为“HTML 至 RSS”联合搜索转换器添加代码
备注
本主题介绍 Infrastructure Update for Microsoft Office Servers中的功能。若要下载此更新,请参阅 2008 年 7 月 15 日版 SharePoint Server 2007 基础结构更新说明。
以下代码将用户查询传递给 Windows Live Search 网站,然后将生成的 HTML 转换成 RSS 源。
HTML to RSS Federated Search Connector中提供了本示例的完整代码。
创建 RSS 源
在 Default.aspx 文件中,更改 Inherits 页属性,以便该属性使用您将在 Default.aspx.cs(代码隐藏)文件中创建的新类。
备注
要让下面的代码正常工作,将加载和显示源的页的名称必须是 Default.aspx。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="SearchHTMLToRSS" %>
在 Default.aspx.cs 文件中,添加以下命名空间指令。
using System; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Text; using System.IO; using HtmlAgilityPack;
修改默认的类声明,使该类声明使用本解决方案中所使用的类名称。
public partial class SearchHTMLToRSS : System.Web.UI.Page
用以下代码替换默认的 Page_Load 方法。
protected override void Render(HtmlTextWriter writer) { //Retrieve query term from query string; construct search URL string queryTerm = Request.QueryString["q"]; string searchURL = string.Format("http://search.live.com/results.aspx?q={0}", queryTerm); Response.ContentType = "text/xml"; //Write the RSS document to the HTMLTextWriter object writer.Write(GetResultsXML(searchURL, queryTerm)); }
为 GetResultsXML 方法添加代码,该方法将查询搜索网站并利用生成的 HTML 来创建 RSS 文档。在找到包含结果的 div 类之后,此代码将提取它所需要的信息并创建包含该信息的 RSS 源。
private string GetResultsXML(string searchURL, string queryTerm) { //Construct and execute the HTTP request HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(searchURL); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //Begin writing the RSS document StringBuilder resultsXML = new StringBuilder(); resultsXML.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); resultsXML.Append("<rss version=\"2.0\">"); resultsXML.AppendFormat("<channel><title><![CDATA[HTML to RSS Conversion: {0}]]></title><link/><description/><ttl>60</ttl>", queryTerm); try { HtmlWeb hw = new HtmlWeb(); HtmlDocument doc = hw.Load(searchURL); //Find the <div> tag that contains the results HtmlNodeCollection nodeCollection = doc.DocumentNode.SelectNodes("//div[@id='results']"); foreach (HtmlNode htmlNode in nodeCollection) { foreach (HtmlNode subNode in htmlNode.ChildNodes) { //Find the list that contains the result items if (subNode.Name == "ul") { foreach (HtmlNode lineItemNode in subNode.ChildNodes) { //Excluding line items that are children of others, because we are interested in the main set of results if (((lineItemNode.Attributes.Count > 0) && (lineItemNode.Attributes[0].Value != "child")) || (lineItemNode.Attributes.Count == 0)) { StringWriter descWriter = new StringWriter(); StringWriter titleWriter = new StringWriter(); StringWriter linkWriter = new StringWriter(); //After retrieving the values sought from the markup, HTML-encode //the strings to avoid validation errors string description = lineItemNode.ChildNodes[1].InnerText; Server.HtmlEncode(description, descWriter); string encDescription = descWriter.ToString(); string title = lineItemNode.FirstChild.FirstChild.InnerText; Server.HtmlEncode(title, titleWriter); string encTitle = titleWriter.ToString(); string link = lineItemNode.FirstChild.FirstChild.Attributes[0].Value; Server.HtmlEncode(link, linkWriter); string encLink = linkWriter.ToString(); if (lineItemNode.FirstChild.FirstChild.Attributes[0].Name == "href") { //Write each RSS item resultsXML.AppendFormat("<item><title>{0}</title><link><![CDATA[{1}]]></link><description>{2}</description></item>", encTitle, encLink, encDescription); } } } } } } } finally { response.Close(); } //Complete RSS document resultsXML.Append("</channel></rss>"); return resultsXML.ToString(); }
将此解决方案部署到您的网站中。若要将此解决方案部署到您的 Office SharePoint Server 2007 网站中,请将此解决方案的内容保存在 _layouts 目录中。有关详细信息,请参阅如何:在 SharePoint 网站中创建 Web 应用程序。
在您的 Web 浏览器或 RSS 阅读器中加载 Default.aspx 文件,以验证它是否将创建 RSS 源。向 URL 添加测试查询字符串(?q=搜索词),以验证源是否将返回结果。