Web design and hosting, database, cloud and social media solutions that deliver business results
  • 商务解决方案
  • 数据库咨询服务
    • 报告
      • Claytabase Server Disk IOPs Calculator
      • SQL代码备份
      • SQL打印机
    • 技术文章
      • SQL-Server
      • SQL Server 2008的维护计划
      • 使用SQL Server函数
      • 使用SQL Server日期
      • 使用SQL Server透视-取消透视
  • 网站设计
    • 怀特岛
    • 技术文章
      • ASP-NET
      • CSS
    • 网站安全
  • 产品展示
  • 社交媒体
  • 关于我们
    • 投资组合
    • 球队
      • 切斯特Copperpot
  • 学院
čeština (CS)Deutsch (DE)English (EN-GB)English (EN-US)Español (ES)Français (FR)हिंदी (HI)italiano (IT)日本語 (JA)polski (PL)Português (PT)русский (RU)Türk (TR)中国的 (ZH)

为您的网站创建一个RSS源

在ASP.NET中为您的网站创建RSS源的简单方法

关于

我花了相当多的时间来看RSS,它可以为我的网站做什么,而第一次有点尝试和错误,我终于到了那里。

我猜,因为你在这里,你可能明白RSS是什么 ,如果不检查链接。

以下是我的旧站点的RSS源中的一个示例,该示例被设置为运行到RSS Atom规范。

Sample RSS

<rss xmlns:atom="https://www.w3.org/2005/Atom" version="2.0">
    <channel>
        <atom:link href="https://www.gsclayton.net/rss" rel="self" type="application/rss+xml"/>
        <title>gsclayton rss feed</title>
        <link>https://www.gsclayton.net</link>
        <copyright>Copyright gsclayton 2012</copyright>
        <description>The la blogs and articles from gsclayton</description>
        <item>
            <title>Disk Space and Database Size Alerts</title>
            <description>
            gsclayton.net-SQL Server 2008, Disk Space and Database Size Alerts stored procedure to check your server
            </description>
            <link>
            https://www.gsclayton.net/Blog/SQL/1/SQL%20Server%202008,%20Disk%20Space%20and%20Database%20Size%20Alerts
            </link>
            <pubDate>Wed, 20 Nov 2013 22:16:08 GMT</pubDate>
            <category>SQL</category>
            <guid>
            https://www.gsclayton.net/8e53acf0-74d8-4e32-a627-f5e71f0fb29f
            </guid>
        </item>
    </channel>
</rss>

关于

代码可能看起来比它复杂得多,所以让我们看看它的功能。

首先我们导入SQL和XML的命名空间。

然后连接到数据库就被设置了,这是在这个例子中将它从web配置中拉出来的。

LoaderVB

VB

Imports System.Data.SqlClient
Imports System.Xml
Imports System.Data
Imports Claytabase.LanguageConvert

Partial Class RSS
  Inherits System.Web.UI.Page
  Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("MySqlConnection").ConnectionString)
  Private Sub LoadFeed() Handles Me.Load
  'Clear any previous output from the buffer
  Dim lg As String = Replace(Page.RouteData.Values("lg").ToString, "'", "''")
  Dim BaseURL As String = "https://www.claytabase.co.uk/"
  Dim MyTitle As String = ConvertText("RSSTitle", lg)
  Dim MyDescr As String = ConvertText("RSSDesc", lg)

  Response.Clear()
  Response.ContentType = "text/xml"
  Response.Charset = "Utf-8"
  Dim xtwFeed As XmlTextWriter = New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
  xtwFeed.WriartDocument()
  'The mandatory rss tag
  xtwFeed.WriartElement("rss")
  xtwFeed.WriteAttriburing("version", "2.0")
  xtwFeed.WriteAttriburing("xmlns:atom", "https://www.w3.org/2005/Atom")
  'The channel tag contains RSS feed details
  xtwFeed.WriartElement("channel")
  xtwFeed.WriteRaw("<atom:link href="https://zh.claytabase.com/"" & BaseURL & lg & "/rss"" rel=""self"" type=""application/rss+xml"" />")
  xtwFeed.WriteElementString("title", MyTitle)
  xtwFeed.WriteElementString("link", baseURL)
  xtwFeed.WriteElementString("copyright", "Copyright Claytabase 2012")
  xtwFeed.WriteElementString("language", lg)
  xtwFeed.WriteElementString("description", MyDescr)

  'Objects needed for connecting to the SQL 
  Using com As New SqlCommand("EXEC GetRSS '" + lg + "'", con)
  If con.State = ConnectionState.Closed Then
  con.Open()
  Else
  End If
  Using dr = com.ExecuteReader()
  'Loop through the content of the database and add them to the RSS feed 
  While dr.Read()
  xtwFeed.WriartElement("item")
  xtwFeed.WriteElementString("title", dr.Item(0).ToString())
  xtwFeed.WriteElementString("description", dr.Item(1).ToString())
  xtwFeed.WriteElementString("link", BaseURL + dr.Item(2).ToString())
  xtwFeed.WriteElementString("pubDate", Format(CDate(dr.Item(3).ToString()), "ddd, dd MMM yyyy hh:mm:ss") + " GMT")
  xtwFeed.WriteElementString("category", dr.Item(4).ToString())
  xtwFeed.WriteElementString("guid", BaseURL + "/" + dr.Item(5).ToString())
  xtwFeed.WriteEndElement()
   End While
  End Using
  End Using
  'Close all tags 
  xtwFeed.WriteEndElement()
  xtwFeed.WriteEndElement()
  xtwFeed.WriteEndDocument()
  xtwFeed.Flush()
  xtwFeed.Close()
  Response.End()
  End Sub
End Class

包起来

从页面加载的代码,这里是一个更有创意的地方。

该字段是本网站的基础部分,告诉系统每个请求使用哪种语言,并且基本URL将是一致的,这些可以在后面的脚本中重新使用。

接下来的两个字段(MyTitle和MyDescr)通过一个内置的语言转换器进行放置,其中数据库返回一个取决于语言输入的字符串。

接下来的几行代码设置了编码和响应类型,打开一个XML写入器并设置一些所需的标题,因为这些标题很少会改变,我已经手动设置了这些。

我们现在可以移动到读取数据,所以首先要创建一个SQL命令,在这种情况下,我只需调用一个存储过程,它根据语言输入从数据库返回所需的字段。

然后,我们打开SQL连接,并声明一个数据读取器循环遍历数据库中的结果集。

我们已经知道XML标签是每个文档的项目,所以我们可以立即打开它。

然后使用数据填充每个必需项目,并确保您的日期格式正确,然后我们将使用WriteEndElement关闭该标签。

一旦数据全部被读取,代码就是关闭数据读取器,连接和编写前面打开的每个元素的结束标签。

一旦您编写并发布了您的内容,请务必在W3C RSS验证器上进行检查。

作者

Helpful?

Please note, this commenting system is still in final testing.
Copyright Claytabase Ltd 2020

Registered in England and Wales 08985867

RSSLoginLink Cookie政策网站地图

Social Media

facebook.com/Claytabaseinstagram.com/claytabase/twitter.com/Claytabaselinkedin.com/company/claytabase-ltd

Get in Touch

+442392064871info@claytabase.comClaytabase Ltd, Unit 3d, Rink Road Industrial Estate, PO33 2LT, United Kingdom
此网站上的设置设置为允许所有Cookie。 这些可以在我们的Cookie政策和设置页面上更改。继续使用本网站即表示您同意使用Cookie。
Ousia Logo
Logout
Ousia CMS Loader