2022年12月24日 星期六

[研究][ASP.NET]RSS (用 FileStream 和 StreamWriter)

[研究][ASP.NET]RSS (用 FileStream 和 StreamWriter)

2022-12-23

RSS(英文全稱:RDF Site Summary 或 Really Simple Syndication),中文譯作簡易資訊聚合,也稱聚合內容,是一種訊息來源格式規範,用以聚合多個網站更新的內容並自動通知網站訂閱者。使用 RSS 後,網站訂閱者便無需再手動檢視網站是否有新的內容,同時 RSS 可將多個網站更新的內容進行整合,以摘要的形式呈現,有助於訂閱者快速獲取重要資訊,並選擇性地點閱檢視。

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C#

********************************************************************************

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
    Inherits="WebApplication1.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <a href="myfeed.xml">myfeed.xml</a>
        </div>
    </form>
</body>
</html>


Default.aspx.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FileStream fs = null;
            StreamWriter sw = null;

            try
            {
                fs = new FileStream(Server.MapPath(".") + "\\myfeed.xml", System.IO.FileMode.Create,

                System.IO.FileAccess.Write);
                sw = new StreamWriter(fs);

                // Write header of RSS file.
                sw.WriteLine("<?xml version=\"1.0\"?>");
                sw.WriteLine("<rss version=\"2.0\" xml:base=\"http://www.myWebSite.com\">");
                sw.WriteLine("<channel>");
                sw.WriteLine("<title>My RSS Feed Title</title>");
                sw.WriteLine("<link>http://www.myWebsite.com</link>");
                sw.WriteLine("<description>My feed description</description>");
                sw.WriteLine("<language>en-us</language>");
                sw.WriteLine("<image>");
                sw.WriteLine("<title>My Title</title>");

                sw.WriteLine("<url>http://www.myWebsite.com/image.gif</url>");
                sw.WriteLine("<link>http://www.myWebsite.com</link>");
                sw.WriteLine("<width>50</width>");
                sw.WriteLine("<height>60</height>");
                sw.WriteLine("</image>");
                // Done adding header of RSS file.

                // Begin adding items (article content).
                sw.WriteLine("<item>");
                sw.WriteLine("<title>Title of article</title>");
                sw.WriteLine("<link>http://www.myWebsite.com/page1.html</link>");
                sw.WriteLine("<description><![CDATA[Body of content goes here.<BR>HTML text can be included inside this tag.<BR>]]></description>");
                sw.WriteLine("<pubDate>" + System.DateTime.Now.ToString("r") + "</pubDate>");
                sw.WriteLine("</item>");
                // End adding item (you can continue adding items here).

                // Write footer of RSS feed.
                sw.WriteLine("</channel>");
                sw.WriteLine("</rss>");
            }
            catch (Exception exp)
            {
                //Debug.WriteLine("Error: " + exception.Message);
            }
            finally
            {
                sw.Close();
                fs.Close();
            }
        }
    }
}




敝人測試,這種作法有個缺點,輸入的文字是靜態確認的,如果參雜了不確定內容的字串變數,會出錯。(例如從資料庫抓出的資料)(有點怪,待研究)

(完)

相關

RSS - 維基百科,自由的百科全書
https://zh.m.wikipedia.org/zh-tw/RSS

System.ServiceModel.Syndication 命名空間
https://learn.microsoft.com/zh-tw/dotnet/api/system.servicemodel.syndication?view=dotnet-plat-ext-7.0

Rss20FeedFormatter 類別 (System.ServiceModel.Syndication) | Microsoft Learn
https://learn.microsoft.com/zh-tw/dotnet/api/system.servicemodel.syndication.rss20feedformatter?view=dotnet-plat-ext-7.0


沒有留言:

張貼留言