2022年12月24日 星期六

[研究][ASP.NET]RSS (用 XmlDocument)

[研究][ASP.NET]RSS (用 XmlDocument)

2022-12-23
2023-01-06 更新,日期改用 ISO 8601

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.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace NICSFrontWebApplication
{
    public partial class RSSService : BaseWebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RSS1(sender, e);
        }

        #region == 新聞 RSS  ==
        protected void RSS1(object sender, EventArgs e)
        {
            try
            {
                // Create the XmlDocument.
                XmlDocument doc = new XmlDocument();

                // rss 加到
                XmlElement rss = doc.CreateElement("rss");
                rss.SetAttribute("version", "2.0");
                doc.AppendChild(rss);

                // channel 加到 rss 下
                XmlElement channel = doc.CreateElement("channel");
                rss.AppendChild(channel);

                XmlElement channelTitle = doc.CreateElement("title");
                channelTitle.InnerText = "新聞";
                channel.AppendChild(channelTitle);

                XmlElement channelLink = doc.CreateElement("link");
                channelLink.InnerText = "https://www.myWebSite.tw/";
                channel.AppendChild(channelLink);

                XmlElement channelDesc = doc.CreateElement("description");
                channelDesc.InnerText = "";
                channel.AppendChild(channelDesc);

                //string xPathExpression = "/rss/channel/item";
                //XmlNodeList nodelist = doc.SelectNodes(xPathExpression);

                //doc.LoadXml("<item><name>wrench</name></item>");
                //doc.LoadXml("<item></item>");
                //doc.LoadXml("<rss version=\"2.0\"><channel><title>新聞</title><link>https://www.myWebSite.tw/</link>");
                //doc.LoadXml("<item><name>wrench</name></item>");
                //doc.LoadXml("</channel></rss>");

                //XmlElement newElem = doc.CreateElement("rss");
                //newElem.SetAttribute("version", "2.0");
                //newElem.InnerText = "";
                //doc.DocumentElement.AppendChild(newElem);

                // Add a price element.
                //XmlElement newElem = doc.CreateElement("price");
                //newElem.InnerText = "10.95";
                //doc.DocumentElement.AppendChild(newElem);

                string queryString = @"SELECT 
[seq]
 ,rssTitle
  ,rssDesc
   ,CONVERT(NVARCHAR(30), rssDate, 126)
FROM [MyTable] "; using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["NICSDBConnectionString"].ConnectionString)) { SqlCommand command = new SqlCommand(queryString, connection); connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { string seq = reader["seq"].ToString(); string rssTitle = reader["rssTitle"].ToString(); string rssDesc = reader["rssDesc"].ToString(); string rssDate = reader["rssDate"].ToString(); XmlElement item = doc.CreateElement("item"); channel.AppendChild(item); XmlElement linkElem = doc.CreateElement("link"); linkElem.InnerText = "https://www.myWebSite.tw/News.aspx?seq=" + seq; item.AppendChild(linkElem); XmlElement authorElem = doc.CreateElement("author"); authorElem.InnerText = "webmaster@myWebSite.tw"; item.AppendChild(authorElem); XmlElement titleElem = doc.CreateElement("title"); titleElem.InnerText = rssTitle; item.AppendChild(titleElem); XmlElement descElem = doc.CreateElement("description"); descElem.InnerText = rssDesc; item.AppendChild(descElem); XmlElement dateElem = doc.CreateElement("pubDate"); dateElem.InnerText = rssDate; item.AppendChild(dateElem); } } } XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; // Save the document to a file and auto-indent the output. XmlWriter writer = XmlWriter.Create(Server.MapPath(".") + "\\myfeed.xml", settings); doc.Save(writer); } catch (Exception) { //throw; } } #endregion == 新聞 RSS == } } }



(完)

相關

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

[研究]SQL Server 2019 T-SQL 取得 ISO 8601標準之日期時間
http://shaurong.blogspot.com/2023/01/sql-server-2019-t-sql-iso-8601.html


沒有留言:

張貼留言