剛親自撰寫一篇技術文章,請大家多指教。哈哈。
原文首發:http://bbs.5inet.Net/topic.ASPx?topicid=181
有的時候我們需要在網頁裡讀取論壇的信息,在傳統ASP的時候我們使用的是JS或者是IFRAME,這兩種方式都不是很方便,而且對搜索引擎不友好。現在有了.Net,我們有了另一種方式。
要求:論壇需要提供RSS支持。
代碼如下:
#region task class
//這是一個任務類,執行具體的任務
public class RssAsyncTask
{
private String _rssContent;
private AsyncTaskDelegate _dlgt;
private string rssUrl;
private bool _success;
public bool IsSuccess
{
get
{
return _success;
}
}
public RssAsyncTask(string rssUrl)
{
this.rssUrl = rssUrl;
}
// Create delegate.
protected delegate void AsyncTaskDelegate();
public String GetRssContent()
{
return _rssContent;
}
public void DoTheAsyncTask()
{
// Introduce an artificial delay to simulate a delayed
// asynchronous task. Make this greater than the
// AsyncTimeout property.
WebClient wc = new WebClIEnt();
try
{
_rssContent = wc.DownloadString(rssUrl);
_success = true;
}
catch (Exception e)
&
nbsp; {
_rssContent = e.Message;
}
finally
{
wc.Dispose();
}
//Thread.Sleep(TimeSpan.FromSeconds(5.0));
}
// Define the method that will get called to
// start the asynchronous task.
public IAsyncResult OnBegin(object sender, EventArgs e,
AsyncCallback cb, object extraData)
{
//_rssContent = "Beginning async task.";
_dlgt = new AsyncTaskDelegate(DoTheAsyncTask);
IAsyncResult result = _dlgt.BeginInvoke(cb, extraData);
return result;
}
// Define the method that will get called when
// the asynchronous task is ended.
public void OnEnd(IAsyncResult ar)
{
//_rssContent = "Asynchronous task completed.";
_dlgt.EndInvoke(ar);
}
// Define the method that will get called if the task
// is not completed within the asynchronous timeout interval.
public void OnTimeout(IAsyncResult ar)
{
_rssContent = "Ansynchronous task failed to complete " +
"because it exceeded the AsyncTimeout parameter.";
}
}
#endregion
//一個自定義的控件,繼承自另一個自定義控件。
public class RArticle
: LPanel
{
#region propertIEs
string rssUrl;
public string RssUrl
{
get { return rssUrl; }
set { rssUrl = value; }
}
int maxRecordNumber = 6;
public int MaxRecordNumber
{
get { return maxRecordNumber; }
set { maxRecordNumber = value; }
}
#endregion
RssAsyncTask task;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
task = new RssAsyncTask(this.rssUrl);
PageAsyncTask asyncTask = new PageAsyncTask(task.OnBegin, task.OnEnd, task.OnTimeout, null);
Page.RegisterAsyncTask(asyncTask);
Page.ExecuteRegisteredAsyncTasks();
}
static Random r = new Random();
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
string rssContent = task.GetRssContent();
XMLDocument doc = null;
if (task.IsSuccess)
{
doc = new XMLDocument();
doc.LoadXML(rssContent);
this.Title = doc.SelectSingleNode("rss/channel/title").InnerText;
&n
bsp; this.TitleNavigateUrl = doc.SelectSingleNode("rss/channel/link").InnerText;
this.ShowTitle = true;
}
base.RenderBegin(writer);
writer.WriteBeginTag("div");
writer.WriteAttribute("class", "child2");
Right(writer);
writer.WriteBeginTag("ul");
Right(writer);
if (doc != null)
{
#region success
XMLNodeList items = doc.SelectNodes("rss/channel/item");
List<XmlNode> nodes = new List<XMLNode>();
foreach (XMLNode node in items)
nodes.Add(node);
//使用范型進行日期的倒序排列
nodes.Sort(new Comparison<XmlNode>(delegate(XmlNode n1, XMLNode n2)
{
DateTime d1 = DateTime.Parse(n1.SelectSingleNode("pubDate").InnerText);
DateTime d2 = DateTime.Parse(n2.SelectSingleNode("pubDate").InnerText);
TimeSpan ts = d2 - d1;
return (int)ts.TotalSeconds;
}));
for (
int i = 0; i < maxRecordNumber; i++)
{
XMLNode node = nodes[i];
writer.WriteBeginTag("li");
Right(writer);
writer.WriteBeginTag("a");
writer.WriteAttribute("target", "_blank");
writer.WriteAttribute("href", node.SelectSingleNode("link").InnerText);
Right(writer);
writer.Write(node.SelectSingleNode("title").InnerText);
writer.WriteEndTag("a");
writer.WriteEndTag("li");
}
#endregion
}
else
{
writer.WriteBeginTag("pre");
Right(writer);
writer.Write(task.GetRssContent());
writer.WriteEndTag("pre");
}
writer.WriteEndTag("ul");
writer.WriteEndTag("div");
RenderChildren(writer);
base.RenderEnd(writer);
}
}
使用方法:
一、注冊控件
<%@ Register Assembly="Controls" Namespace="Limited.Controls" TagPrefix="lm" %>
二、調用
<lm:RArticle ID="RArticle1" runat="server" MaxRecordNumber="10" RssUrl="http://bbs.5inet.Net/rss.ASPx" />
為了簡便起見,本程序就沒有使用緩存之類的技術了,如有必要,請自行添加。