65.9K
CodeProject 正在变化。 阅读更多。
Home

一个非常标准、强大且易于使用的 RSS 2.0 代码示例和库

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.85/5 (20投票s)

2005年2月18日

MIT

1分钟阅读

viewsIcon

48768

downloadIcon

649

使用此库,您可以轻松地为您的网站创建一些 RSS 2.0 文件,以及将其他网站的 RSS 2.0 文件用于您的网站。

Sample Image - Working_on_RSS_20.jpg

引言

使用此库,您可以轻松地为您的网站创建一些 RSS 2.0 文件,以及使用(显示)其他网站的 RSS 2.0 文件到您的网站。

创建 RSS 2.0

创建 RSS 2.0 示例 (1)

要查看此源代码,您应该查看 create_rss2_sample1.aspx.cs 文件。 在此源代码中,您可以看到我创建了一个 RSSChannel 对象,然后是所需的 RSSRoot 对象。 之后,我以三种方式将一些 RSSItem(s) 添加到 oRSSRoot 项目(作为集合)。

private void Page_Load(object sender, System.EventArgs e)
  {
   IranianExperts.RSS.RSSChannel oRSSChannel =
     new IranianExperts.RSS.RSSChannel("Channel Title", 
                "Channel Link", "Channel Description");

   oRSSChannel.PubDate = System.DateTime.Now.ToString();

   IranianExperts.RSS.RSSRoot oRSSRoot =
     new IranianExperts.RSS.RSSRoot(oRSSChannel,
                Response.OutputStream);

   IranianExperts.RSS.RSSItem oRSSItem = null;

   oRSSItem = new IranianExperts.RSS.RSSItem("Item 1", "http://www.item1.com/");
   oRSSItem.PubDate = System.DateTime.Now.ToString();
   oRSSRoot.Items.Add(oRSSItem);

   oRSSRoot.Items.Add("Item 2");

   oRSSRoot.Items.Add("Item 3", "http://www.item3.com/");

   Response.Clear();
   Response.ContentEncoding = System.Text.Encoding.UTF8;
   Response.ContentType = "text/xml";
   IranianExperts.RSS.RSSUtilities.PublishRSS(oRSSRoot);
   Response.End();
  }

创建 RSS 2.0 示例 (2)

要查看此源代码,您应该查看 create_rss2_sample2.aspx.cs 文件。 此示例与创建 RSS 2.0 示例 (1) 非常相似,但在此示例中,我创建了另一个名为 oRSSImage 的对象。 如果您想为您的 RSS 2.0 添加一些额外信息,例如您的网站图像及其属性,最好创建此对象并使用它。

private void Page_Load(object sender, System.EventArgs e)
  {
   IranianExperts.RSS.RSSChannel oRSSChannel =
     new IranianExperts.RSS.RSSChannel("Channel Title",
                "Channel Link", "Channel Description");

   oRSSChannel.PubDate = System.DateTime.Now.ToString();


   IranianExperts.RSS.RSSImage oRSSImage =
     new IranianExperts.RSS.RSSImage(
    "http://www.site.com/images/banner.gif",
        "http://www.iranianexperts.com/", "Iranian Experts");

   IranianExperts.RSS.RSSRoot oRSSRoot =
     new IranianExperts.RSS.RSSRoot(oRSSChannel,
            oRSSImage, Response.OutputStream);

   IranianExperts.RSS.RSSItem oRSSItem = null;

   oRSSItem =
     new IranianExperts.RSS.RSSItem("Item 1", "http://www.item1.com/");
   oRSSItem.PubDate = System.DateTime.Now.ToString();
   oRSSRoot.Items.Add(oRSSItem);

   oRSSRoot.Items.Add("Item 2");

   oRSSRoot.Items.Add("Item 3", "http://www.item3.com/");

   Response.Clear();
   Response.ContentEncoding = System.Text.Encoding.UTF8;
   Response.ContentType = "text/xml";
   IranianExperts.RSS.RSSUtilities.PublishRSS(oRSSRoot);
   Response.End();
  }

在您的网站中显示 RSS 2.0 文件

为了在一些 .aspx 文件中显示一些 RSS 2.0 文件,您必须放置一个 DataGrid 并设置其属性

<asp:datagrid id="grdData" cellpadding="3"
         autogeneratecolumns="False" font-name="Tahoma"
         font-size="8pt" runat="server" bordercolor="#E7E7FF"
         borderstyle="None" borderwidth="1px" backcolor="White"
         gridlines="Horizontal" font-names="Tahoma">

     <alternatingitemstyle backcolor="#F7F7F7">
     </alternatingitemstyle>



     <itemstyle forecolor="#4A3C8C" backcolor="#E7E7FF">
     </itemstyle>


     <headerstyle font-size="8pt" font-bold="True"
     horizontalalign="Center" forecolor="#F7F7F7" backcolor="#4A3C8C">
     </headerstyle>


     <columns>
      <asp:templatecolumn headertext="Some Recent Titles Posts...">
       <itemtemplate>
       </itemtemplate>
      </asp:templatecolumn>
     </columns>
    </asp:datagrid>

并引发 DataGridItemDataBound 事件以进行自定义格式化

private void grdData_ItemDataBound(object sender,
         System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   switch(e.Item.ItemType)
   {
    case System.Web.UI.WebControls.ListItemType.Item:
    case System.Web.UI.WebControls.ListItemType.SelectedItem:
    case System.Web.UI.WebControls.ListItemType.AlternatingItem:
     System.Data.DataRowView oDataRowView =
             (System.Data.DataRowView) e.Item.DataItem;


     string strLINK = oDataRowView["LINK"].ToString();
     string strTITLE = oDataRowView["TITLE"].ToString();

     int intTitleMaxLenght = 20;

     if(strLINK == "")
     {
      if(strTITLE.Length <= intTitleMaxLenght)
       e.Item.Cells[0].Text = strTITLE;
      else
       e.Item.Cells[0].Text =
         strTITLE.Substring(0, intTitleMaxLenght - 1) + " ...";
     }
     else
     {
      if(strTITLE.Length <= intTitleMaxLenght)
       e.Item.Cells[0].Text =
     "<a href='" + strLINK + "' target='_blank'>" + strTITLE + "</a>";
      else
       e.Item.Cells[0].Text =
     "<a href='" + strLINK + "' target='_blank'>" +
     strTITLE.Substring(0, intTitleMaxLenght - 1) + " ...</a>";
     }

     break;
   }
  }

因此,根据您希望显示 RSS 2.0 文件的方式(从本地驱动器或 URL 地址),您必须编写以下其中一个源代码,它将对您有用。

对于本地驱动器(在您的网站中)

     strPathName = Server.MapPath("rss") + "file://RSS2Sample1.xml/";
     grdData.DataSource =
     IranianExperts.RSS.RSSUtilities.GetRSSFeed(
        IranianExperts.RSS.RSSLocation.Drive,
         strPathName, IranianExperts.RSS.RSSFeedType.item);
     grdData.DataBind();

对于 URL 地址(使用其他网站的一些 RSS 2.0 文件)

     strPathName = "https:///working_on_rss/rss/RSS2Sample1.xml";
     grdData.DataSource =
     IranianExperts.RSS.RSSUtilities.GetRSSFeed(
        IranianExperts.RSS.RSSLocation.URL,
         strPathName, IranianExperts.RSS.RSSFeedType.item);
     grdData.DataBind();

     strPathName = "https:///working_on_rss/create_rss2_sample1.aspx";
     grdData.DataSource =
     IranianExperts.RSS.RSSUtilities.GetRSSFeed(
        IranianExperts.RSS.RSSLocation.URL,
         strPathName, IranianExperts.RSS.RSSFeedType.item);
     grdData.DataBind();

我希望这些源代码对您有用。 使用它们并享受吧。

祝您一切顺利。

© . All rights reserved.