Tuesday, February 14, 2012

Display Yahoo news using RSS feed in ASP.NET


 Display Yahoo news using ASP.Net

Copy and paste the below code to Display Yahoo news in your web page



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
using System.Xml;

public partial class web_service : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            string rss = "http://rss.news.yahoo.com//rss//entertainment";
            FetchRssFeeds(rss);
        }
        catch (Exception ex)
        {
            Response.Write("Exception:" + ex.Message);
        }
    }

    public void FetchRssFeeds(string rss)
    {
        // Read the RSS feed
        WebRequest rssRequest = WebRequest.Create(rss);
        WebResponse rssResponse = rssRequest.GetResponse();
        Stream rssStream = rssResponse.GetResponseStream();

        // Load XML Document
        XmlDocument rssDocument = new XmlDocument();
        rssDocument.Load(rssStream);

        XmlNodeList rssList = rssDocument.SelectNodes("rss/channel/item");

        string title = "";
        string link = "";
        string description = "";

        Response.Write("<table border=0 width=700 height=900 align=center>");

        int j = 2;

        // Loop through RSS Feed items
        for (int i = 0; i < rssList.Count; i++)
        {
            XmlNode rssDetail;

            rssDetail = rssList.Item(i).SelectSingleNode("title");
            if (rssDetail != null)
            {
                title = rssDetail.InnerText;
            }
            else
            {
                title = "";
            }

            rssDetail = rssList.Item(i).SelectSingleNode("link");
            if (rssDetail != null)
            {
                link = rssDetail.InnerText;
            }
            else
            {
                link = "";
            }

            rssDetail = rssList.Item(i).SelectSingleNode("description");
            if (rssDetail != null)
            {
                description = rssDetail.InnerText;
            }
            else
            {
                description = "";
            }
    
            string color;
            if ((j % 2) == 0)
            {
                color = "#C0FFC0";
                j++;
            }
            else
            {
                color = "#FFE0C0";
                j++;
            }

            Response.Write("<tr bgcolor=" + color + ">");
            Response.Write("<td width=700>");
            Response.Write("<font color=red>" + "<b>" + "<a href=" + link + ">" + title + "</a>" + "</b>" + "</font>");
            Response.Write("</td>");
            Response.Write("</tr>");

            /* Response.Write("<tr bgcolor="+color+">");
             Response.Write("<td width=600>");
             Response.Write("<font color=green>"+ link +"</font>");
             Response.Write("</td>");
             Response.Write("</tr>");  */

            Response.Write("<tr bgcolor=" + color + ">");
            Response.Write("<td width=700>");
            Response.Write("<font color=blue>" + description + "</font>");
            Response.Write("</td>");
            Response.Write("</tr>");


        }
    }
}

1 comment:

  1. Not Working...error (The remote server returned an error: (404) Not Found.

    ReplyDelete