Web design and hosting, database, cloud and social media solutions that deliver business results
  • ビジネスソリューション
    • データベースサービス
      • サーバーのアップグレードと DBA サービス
      • データウェアハウス サービス
      • データ統合
      • パワーBI
    • ウェブサイトデザイン
      • Web サイトのセキュリティ
      • Web サイトの最適化
      • ロゴデザイン
      • 支払いゲートウェイ
      • テクニカルツール
    • ビジネスサービス
      • Google クラウド サービス
      • アマゾン ウェブ サービス
      • マイクロソフト アズール
    • ソーシャルメディア
    • マイクロソフトオフィス
  • 学校
    • テスト環境
    • 学習データベース
      • 基礎
      • SQL Serverデータ
      • SQLServerのメンテナンス
      • SQL Serverの日付の使用
      • SQLServerピボットの使用-ピボット解除
      • SQLServer関数の使用
      • オープンクエリを取得
      • ツール
    • ウェブデザインを学ぶ
      • Ousia コンテンツ管理システムの構築
      • ASP-NET の使用
      • CSS の使用
      • JavaScript の使用
    • クラウドと IT サービスの学習
      • タスク スケジューラ エラー 2147943645
      • OpenSSL での SSL の要求と PFX ファイルの生成の簡単な手順
    • ソーシャル メディアの使用
      • Facebookアカウントを個人用からビジネス用に変更する
      • Google レビューを依頼する
      • ソーシャル メディアの取り組みをどこに集中させるかを選択する
      • ソーシャル メディアの画像サイズ
      • メタ データを使用してソーシャル メディア画像を設定する
  • 私たちに関しては
    • ブログ
      • Google コア アップデート 2020 年 1 月
      • インターネット上のウェブサイトに関する最も厄介なこと
      • ウェブサイトの広告リーフレットのコンテンツを選択する方法
      • エントリーレベルのゲーム機の構築
      • オンライン詐欺の防止
      • ギグエコノミーのスキマー
      • ホットチリインターネット閉鎖
      • 無料のベクター グラフィックスのトップ 5 Web サイト
    • キャリア
      • 翻訳者 英日
      • 英語-トルコ語翻訳者
    • チーム
      • アイセ・ハー
      • アリ アル アミン
      • ギャビン・クレイトン
      • サイガングー
      • スーリヤ・ムッカマラ
      • スニール・クマール
      • チェスターコッパーポット
    • ポートフォリオ
č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)

データベースから Web サイトの RSS フィードを作成する

チュートリアル: SQL Server データベースから VB.NET または C#.NET を使用して、ASP.NET で Web サイト用の多言語 RSS フィードを作成する

Claytabaseによるウェブサイトのデザイン

このウォークスルーを実行するには、 テスト環境の設定に関するガイドに従うか、ニーズに合わせてコードを調整してください。

経験レベル - 中級

約

RSS アイコン

RSS は、サイトに加えられた更新に関する情報を提供する標準化された XML ベースのファイルです。

これらは、外出してユーザーのさまざまなお気に入りのサイトの変更をチェックし、何か新しいことを通知するさまざまなニュース集約アプリケーションで使用できます。

Web の開発の性質上、わずかに異なる形式がいくつかあります。以下の形式は Atom 2.0 のものです。

Sample RSS File

<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">  <channel>    <atom:link href="https://ja.claytabase.com/" rel="self" type="application/rss+xml"/>    <title/>    <link/>    <copyright>Copyright Claytabase 2012</copyright>    <language/>    <description/>    <item>      <title>Create an RSS Feed for your Website In ASP NET and SQL Server</title>      <description>Easy way to create an RSS Feed for your Website in ASP.NET in VB.NET or C#.NET</description>      <link>https://www.claytabase.co.uk/Academy/Learning-Web-Design/Using-ASP-NET/Create-an-RSS-Feed-for-your-Website-In-ASP-NET-and-SQL-Server</link>      <pubDate>Wed, 15 Dec 2021 09:37:24 GMT</pubDate>      <category>monthly</category>      <guid>https://www.claytabase.co.uk/A7038527-90D0-4214-8C65-3A2BD831F141</guid>    </item>    <item>      <title>CSS Styling for AJAX Accordion Control</title>      <description>Some simple CSS styling rules for an AJAX Accordion Control</description>      <link>https://www.claytabase.co.uk/Academy/Learning-Web-Design/Using-CSS/CSS-Styling-for-AJAX-Accordion-Control</link>      <pubDate>Tue, 14 Dec 2021 07:00:00 GMT</pubDate>      <category>monthly</category>      <guid>https://www.claytabase.co.uk/0DB19797-5B3A-45F0-B3E6-2A8080DA60EE</guid>    </item>  </channel></rss>
We've created a sample table, and run this through in our Academy Database. This table will include a UNIQUEIDENTIFIER (GUID), Title, Description, URL, language and date, this data would be the sort of information captured or required in most CMS's.
USE ClaytabaseAcademyGOCREATE TABLE RSSPages(PageGUID UNIQUEIDENTIFIER CONSTRAINT DF_PageGUID DEFAULT NEWSEQUENTIALID() CONSTRAINT PK_PageGUID PRIMARY KEY,PageTitle NVARCHAR(200),PageDescription NVARCHAR(500),PageURL NVARCHAR(500),PageLanguage NVARCHAR(2),PageDate DATETIME,ChangeFrequency NVARCHAR(20))GOINSERT INTO RSSPages(PageTitle,PageDescription,PageURL,PageDate,PageLanguage,ChangeFrequency)SELECT 'Create an RSS Feed for your Website from a database','Walkthrough: Creating an RSS Feed for your Website in ASP.NET using VB.NET or C#.NET from an SQL Server database','https://www.claytabase.co.uk/Academy/Learning-Web-Design/Using-ASP-NET/Create-an-RSS-Feed-for-your-Website-In-ASP-NET-and-SQL-Server','2021-12-15 10:00:00','en','Weekly'INSERT INTO RSSPages(PageTitle,PageDescription,PageURL,PageDate,PageLanguage,ChangeFrequency)SELECT 'CSS Styling for AJAX Accordion Control','Some simple CSS styling rules for an AJAX Accordion Control','https://www.claytabase.co.uk/Academy/Learning-Web-Design/Using-CSS/CSS-Styling-for-AJAX-Accordion-Control',GETDATE(),'en','Monthly'INSERT INTO RSSPages(PageTitle,PageDescription,PageURL,PageDate,PageLanguage,ChangeFrequency)SELECT 'We''ll take the strain while you do what you are good at','A Multi-National team with over 20 years of experience specialising in Web, Database, Cloud services and bespoke Business Management Software','https://www.claytabase.co.uk/',GETDATE(),'en','Daily'INSERT INTO RSSPages(PageTitle,PageDescription,PageURL,PageDate,PageLanguage,ChangeFrequency)SELECT 'Wir nehmen Ihnen die Anstrengung, während Sie das tun, was Sie gut können','Ein multinationales Team mit über 20 Jahren Erfahrung, das sich auf Web-, Datenbank-, Cloud-Dienste und maßgeschneiderte Business-Management-Software spezialisiert hat','https://de.claytabase.com/',GETDATE(),'de','Daily'INSERT INTO RSSPages(PageTitle,PageDescription,PageURL,PageDate,PageLanguage,ChangeFrequency)SELECT 'Nos esforzaremos mientras haces lo que se te da bien','Un equipo multinacional con más de 20 años de experiencia especializado en Web, bases de datos, servicios en la nube y software de gestión empresarial a medida.','https://de.claytabase.com/',GETDATE(),'es','Daily'GOCREATE PROC GetRSSPages(@Language NVARCHAR(2)) AS BEGINSELECT * FROM RSSPagesWHERE PageLanguage=@LanguageENDGOEXEC GetRSSPages 'de'

Visual Studio で新しい Web フォームを追加する

VS で、Pages フォルダーを右クリックし、[追加]、[Web フォーム] の順に選択して、Web フォームを追加します。

コード ビハインドに進みたいので、新しいページを右クリックして [コードの表示] を選択します。

コードはおそらく実際よりもはるかに複雑に見えるので、その機能を見てみましょう。

まず、SQL と XML の名前空間をインポートします。

次に、データベースへの接続が設定されます。これは、このインスタンスの Web 構成から取得されます。

LoaderVBC#

VB

Imports System.Data.SqlClientImports System.XmlPublic Class RSS    Inherits System.Web.UI.Page    Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlConnection").ConnectionString)    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load        Dim RSSLanguage As String = "en"        Dim BaseURL As String = "https://www.claytabase.co.uk/"        Dim MyTitle As String = "Academy Title"        Dim MyDescr As String = "Academy Description"
        'Clear any previous output from the buffer        Response.ClearContent()        Response.ContentType = "text/xml"        Response.Charset = "Utf-8"        Dim xtwFeed As XmlTextWriter = New XmlTextWriter(Response.OutputStream, Encoding.UTF8)        xtwFeed.WriteStartDocument()        'The mandatory rss tag        xtwFeed.WriteStartElement("rss")        xtwFeed.WriteAttributeString("version", "2.0")        xtwFeed.WriteAttributeString("xmlns:atom", "https://www.w3.org/2005/Atom")        'The channel tag contains RSS feed details        xtwFeed.WriteStartElement("channel")        xtwFeed.WriteRaw("<atom:link href="https://ja.claytabase.com/"" & BaseURL & RSSLanguage & "/rss"" rel=""self"" type=""application/rss+xml"" />")        xtwFeed.WriteElementString("title", MyTitle)        xtwFeed.WriteElementString("link", BaseURL)        xtwFeed.WriteElementString("copyright", "Copyright Claytabase 2012")        xtwFeed.WriteElementString("language", RSSLanguage)        xtwFeed.WriteElementString("description", MyDescr)
        'Objects needed for connecting to the SQL         Using com As New SqlCommand("EXEC GetRSSPages '" + RSSLanguage + "'", 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.WriteStartElement("item")                    xtwFeed.WriteElementString("title", dr.Item("PageTitle").ToString())                    xtwFeed.WriteElementString("description", dr.Item("PageDescription").ToString())                    xtwFeed.WriteElementString("link", dr.Item("PageURL").ToString())                    xtwFeed.WriteElementString("pubDate", Format(dr.Item("PageDate"), "ddd, dd MMM yyyy hh:mm:ss") + " GMT")                    xtwFeed.WriteElementString("category", dr.Item("ChangeFrequency").ToString())                    xtwFeed.WriteElementString("guid", BaseURL + "/" + dr.Item("PageGUID").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 SubEnd Class

C#

using System.Text;using Microsoft.VisualBasic;using System.Data.SqlClient;using System.Xml;public class RSS : System.Web.UI.Page{    private SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings("SqlConnection").ConnectionString);    protected void Page_Load(object sender, System.EventArgs e)    {        string RSSLanguage = "en";        string BaseURL = "https://www.claytabase.co.uk/";        string MyTitle = "Academy Title";        string MyDescr = "Academy Description";
        // Clear any previous output from the buffer        System.Web.UI.Page.Response.ClearContent();        System.Web.UI.Page.Response.ContentType = "text/xml";        System.Web.UI.Page.Response.Charset = "Utf-8";        XmlTextWriter xtwFeed = new XmlTextWriter(System.Web.UI.Page.Response.OutputStream, Encoding.UTF8);        xtwFeed.WriteStartDocument();        // The mandatory rss tag        xtwFeed.WriteStartElement("rss");        xtwFeed.WriteAttributeString("version", "2.0");        xtwFeed.WriteAttributeString("xmlns:atom", "https://www.w3.org/2005/Atom");        // The channel tag contains RSS feed details        xtwFeed.WriteStartElement("channel");        xtwFeed.WriteRaw("<atom:link href=\"" + BaseURL + RSSLanguage + "/rss\" rel=\"self\" type=\"application/rss+xml\" />");        xtwFeed.WriteElementString("title", MyTitle);        xtwFeed.WriteElementString("link", BaseURL);        xtwFeed.WriteElementString("copyright", "Copyright Claytabase 2012");        xtwFeed.WriteElementString("language", RSSLanguage);        xtwFeed.WriteElementString("description", MyDescr);
        // Objects needed for connecting to the SQL         using (SqlCommand com = new SqlCommand("EXEC GetRSSPages '" + RSSLanguage + "'", con))        {            if (con.State == ConnectionState.Closed)                con.Open();            else            {            }            using (var dr = com.ExecuteReader())            {                // Loop through the content of the database and add them to the RSS feed                while (dr.Read())                {                    xtwFeed.WriteStartElement("item");                    xtwFeed.WriteElementString("title", dr.Item["PageTitle"].ToString());                    xtwFeed.WriteElementString("description", dr.Item["PageDescription"].ToString());                    xtwFeed.WriteElementString("link", dr.Item["PageURL"].ToString());                    xtwFeed.WriteElementString("pubDate", Strings.Format(dr.Item["PageDate"], "ddd, dd MMM yyyy hh:mm:ss") + " GMT");                    xtwFeed.WriteElementString("category", dr.Item["ChangeFrequency"].ToString());                    xtwFeed.WriteElementString("guid", BaseURL + "/" + dr.Item["PageGUID"].ToString());                    xtwFeed.WriteEndElement();                }            }        }        // Close all tags         xtwFeed.WriteEndElement();        xtwFeed.WriteEndElement();        xtwFeed.WriteEndDocument();        xtwFeed.Flush();        xtwFeed.Close();        System.Web.UI.Page.Response.End();    }}

まとめ

ページの読み込みからコードに移ります。これは、もう少しクリエイティブな部分です。

フィールド RSSLanguage は CMS で使用され、各リクエストに使用されている言語をシステムに伝え、ベース URL も入力されます。この例では、それらを静的フィールドにしました。

次の数行のコードでは、エンコーディングと応答タイプを設定し、XML ライターを開き、必要な見出しをいくつか設定します。これらはめったに変更されないため、手動で設定しました。

データの読み取りに移ることができるので、最初の仕事は SQL コマンドを作成することです。この場合、言語入力に応じてデータベースから必要なフィールドを返すストアド プロシージャを呼び出すだけです。

次に、SQL 接続を開き、データベースからの結果セットをループするデータ リーダーを宣言します。

XML タグが各ドキュメントのアイテムであることは既にわかっているので、すぐに開くことができます。

次に、必要な各項目にデータを入力し、日付が正しい形式であることを確認してから、WriteEndElement を使用してタグを閉じます。

データがすべて読み取られると、コードはデータ リーダーと接続を閉じ、以前に開いた各要素の終了タグを書き込みます。

作成して公開したら、必ずW3C RSS Validator で確認してください。

著者

役に立った?

Please note, this commenting system is still in final testing.

Claytabaseによるウェブサイトのデザイン

これは、当社の Web サイト デザイン サービスの一部である、市場で最も高速で最適化されたシステムの 1 つである Ousia コンテンツ管理システム コードから変更されたコードのセクションです。

これらは、約£500から始まるサイトで利用できます.

詳細: レスポンシブで高速。コンテンツ管理システムを使用した Web 開発、設計、およびホスティング
Copyright Claytabase Ltd 2020

Registered in England and Wales 08985867

RSSLoginLink クッキーポリシーサイトマップ

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
The settings on this site are set to allow all cookies. These can be changed on our Cookie Policy & Settings page.
By continuing to use this site you agree to the use of cookies.
Ousia Logo
Logout
Ousia CMS Loader