Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A very standard, powerful and easy to use Sample and Library for working on RSS 2.0

0.00/5 (No votes)
18 Feb 2005 1  
With this library, you can easily create some RSS 2.0 files for your site and/or use some RSS 2.0 files from other sites into your site.

Sample Image - Working_on_RSS_20.jpg

Introduction

With this library, you can easily create some RSS 2.0 files for your site and/or use (display) some RSS 2.0 files from other sites into your site.

Create RSS 2.0

Create RSS 2.0 Sample (1):

For seeing this source code, you should see the create_rss2_sample1.aspx.cs file. In this source code, you see that I created a RSSChannel object and then a RSSRoot object as required objects. After all, I added some RSSItem(s) to oRSSRoot items (as Collection) in three ways.

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();
  }

Create RSS 2.0 Sample (2):

For seeing this source code, you should see the create_rss2_sample2.aspx.cs file. This sample is too similar to Create RSS 2.0 sample (1), but in this sample I created the other object with the name of oRSSImage. If you want to add some extra information for your RSS 2.0 such as your site image and its properties, it's better to create this object and use it.

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();
  }

Displaying RSS 2.0 Files in Your Site

For displaying some RSS 2.0 files in your some .aspx files, you must put one DataGrid and set its properties:

<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>

And raise ItemDataBound event of your DataGrid for custom formatting:

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;
   }
  }

So, depending on how you want to display RSS 2.0 files (from Local Drive or URL address), you must write one of these source codes that will be useful for you.

For Local Drive (in your site):

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

For URL address (Using some RSS 2.0 files from other sites):

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

or

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

I wish these source codes will be useful for you. Use them and enjoy it.

Best Wishes.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here