Click here to Skip to main content
15,867,686 members
Articles / Web Development / ASP.NET
Article

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

Rate me:
Please Sign up or sign in to vote.
4.85/5 (23 votes)
18 Feb 2005MIT1 min read 45.9K   649   51   4
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.

C#
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.

C#
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.NET
<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:

C#
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):

C#
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):

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

C#
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, along with any associated source code and files, is licensed under The MIT License


Written By
Web Developer Sematec Ins.
Iran (Islamic Republic of) Iran (Islamic Republic of)
My experiences are:

HTML 5.0, CSS 3.0
JQuery, Angular JS, Bootstrap

MVC 5.0, WEB API, c#

My Site URLs:
http://www.IranianExperts.ir
http://www.IranianExperts.com

My Yahoo Group URL: http://groups.yahoo.com/group/iranianexperts

Mobile: 0098-912-108-7461
Address: Tehran, Tehran, Iran

Comments and Discussions

 
GeneralThanks Pin
Anonymous25-Feb-05 17:08
Anonymous25-Feb-05 17:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.