5,693,936 members and growing! (16,498 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Samples     Intermediate License: The MIT License

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

By Dariush Tasdighi

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.
C#, XML.NET 1.1, NT4, Win2K, WinXP, Win2003, Windows, .NET, ADO.NET, WebForms, ASP.NET, Visual Studio, VS.NET2003, Dev

Posted: 18 Feb 2005
Updated: 18 Feb 2005
Views: 24,842
Bookmarked: 40 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 4.88 Rating: 3.96 out of 5
2 votes, 11.8%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 5.9%
4
14 votes, 82.4%
5

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

About the Author

Dariush Tasdighi


I'm experienced in below items:

- XML 1.0
- CSS 2.0
- ASP 3.0
- HTML 4.01
- XHTML 1.0
- Javascript 1.5
- .NET Framework 1.1/2.0
- Microsoft Office 2000/XP
- Microsoft Visual Basic 6
- Microsoft SQL Server 2000/2005
- Microsoft C#.NET (Windows Based)
- Microsoft C#.NET (XML Web Service)
- Microsoft C#.NET (Web Based = ASP.NET)

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

Occupation: Web Developer
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralThanks For Good Solutions Professor TasdighimemberAli Safavi13:39 26 Feb '08  
Generalpretty Good!!memberdavidwei10:28 6 Sep '05  
GeneralThankssussAnonymous18:08 25 Feb '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Feb 2005
Editor: Sumalatha K.R.
Copyright 2005 by Dariush Tasdighi
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project