還記得以前嗎?以前要做頁面上寫個小組件,調用指定(rss輸出地址的)的內容。最好的辦法就是通過XSLT+xml來輸出字符 串。讓我們還是花了一分鐘的時候回憶一下以前的做法吧
曾經的做法:
PRotected Overrides Sub OnLoad(ByVal e As System.EventArgs)
Dim CacheKey As String = Url & "-" & Sort
Dim objCache As System.Web.Caching.Cache = HttpRuntime.Cache
If objCache.Item(CacheKey) Is Nothing Then
Dim xpDocument As XPathDocument = New XPathDocument(Url, XmlSpace.Preserve)
Dim XTran As XslTransform = New XslTransform
XTran.Load(Web.HttpRuntime.AppDomainAppPath & "TransForm.xsl") 'TransForm.xsl
Dim sw As StringWriter = New StringWriter
Dim xArgList As XsltArgumentList = New XsltArgumentList
xArgList.AddParam("Sort", "", Sort)
xArgList.AddParam("strlength", "", strLength)
XTran.Transform(xpDocument, xArgList, sw)
objCache.Insert(CacheKey, sw, Nothing, DateTime.Now.AddMinutes(15), TimeSpan.Zero)
End If
MyBase.Text = CType(objCache.Item(CacheKey), StringWriter).ToString
End SubXSLT轉換文件
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl=" xmlns=" xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:dc=" xmlns:slash=" xmlns:sy=" xmlns:resuls="urn:StrLength">
<xsl:param name="Sort" select="'10'"></xsl:param>
<xsl:param name="strlength" select="'15'"></xsl:param>
<xsl:template match="http://rss">
<xsl:apply-templates select="channel" />
</xsl:template>
<msxsl:script language="VB" implements-prefix="resuls">
<![CDATA[
Function GetSubString(byval origStr As String, byval endIndex As Integer) As String
Dim bytes As Byte() = System.Text.Encoding.GetEncoding("gb2312").GetBytes(origStr)
If endIndex >= bytes.Length Then
Return origStr
End If
Dim subBytes(endIndex) As Byte
Array.Copy(bytes, 0, subBytes, 0, endIndex)
Return System.Text.Encoding.GetEncoding("gb2312").GetString(subBytes) + ""
End Function 'GetSubString
public function strDate(byval s as string)as string
return CDate(s).Day.ToString & "日"
end function
]]>
</msxsl:script>
<xsl:template match="channel">
<table width="300" border="0" cellspacing="0" cellpadding="0">
<xsl:for-each select="item">
<xsl:if test="position() <=$Sort">
<tr>
<td width="21"><img src=">
<td><a href="{link}"><xsl:value-of select="resuls:GetSubString(title,$strlength)"/></a> <xsl:value-of select="resuls:strDate(dc:date)"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
而現在。根據MS的想法。一切只要拖拖控件就能完成我們的所要功能(我一向不喜歡這樣,還是自己DIY好)。所以。下面介紹這款小工具:asp.net RSS Toolkit
RSS TOOLKIT主要是從服務器上裝載RSS輸出(XML格式輸出),生成數據集,然后填充到DATALIST控件當中。另外它支持數據緩存的功能
目前最新版本:1.0.0.1
新的特性:
• Added MaxItems property to RssDataSource to limit the number of items returned.
• Added automatic generation of <link> tags from RssHyperLink control, to light up the RSS
toolbar icon in IE7. For more information please see http://blogs.msdn.com/rssteam/articles/PublishersGuide.aspx
• Added protected Context property (of type HttpContext) to RssHttpHandlerBase class, to allow access to the
HTTP request while generating a feed.
• Added generation of LoadChannel(string url) method in RssCodeGenerator so that one strongly typed
channel class can be used to consume different channels.
• Fixed problem expanding app relative (~/…) links containing query string when generating RSS feeds.
下載地址:點擊下載
詳細說明:http://weblogs.asp.net/scottgu/archive/2006/02/22/438738.aspx
創建新的頁面

為datalist控件創建新的數據源

選擇rssDataSource

輸入RSS地址(我這里以http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml)

根據RSS輸出的格式,進行綁定
OK。現在可以瀏覽一下效果

晚上出去吃晚飯的時候。走出外面,感覺到夏天的來到。呵。我喜歡夏天的這種感覺。帶上好的心情,出去走走。也是一種很好的享受。最后。推薦一首歌:夏天的味道。
新聞熱點
疑難解答