.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Implementing RSS and Atom Feed : Next Part

Posted By:Nikhil Kumar       Posted Date: August 02, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

PART 2 :In today s scenario, Internet surfers frequently download Web pages of their favorite websites and search the content for any updates. These websites range from news portals to job portals. Accessing websites in this way, however, consumes a lot of time since the surfers have to open the websites and browse their favorite pages for the updates.
 

 

Implementing RSS and Atom Feed

 


 

5. Now, add the code in the code-behind file of the Default.aspx page as shown in Listing :

Listing B.3: Showing the code for the Code-Behind File of the Default.aspx Page

 

In VB

 

Imports System

Imports System.Configuration

Imports System.Data

Imports System.Linq

Imports System.Web

Imports System.Web.Security

Imports System.Web.UI

Imports System.Web.UI.HtmlControls

Imports System.Web.UI.WebControls

Imports System.Web.UI.WebControls.WebParts

Imports System.Xml.Linq

Imports System.Data.SqlClient

Imports System.Text

Imports System.Xml

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

Try

Response.Clear()

Response.ContentType = "text/xml"

Dim XMLTextObject As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)

XMLTextObject.WriteStartDocument()

XMLTextObject.WriteStartElement("rss")

XMLTextObject.WriteAttributeString("version", "2.0")

XMLTextObject.WriteStartElement("channel")

XMLTextObject.WriteElementString("title", "Creating RSS Feed On a Website")

XMLTextObject.WriteElementString("link", "http://www.somedomain.com/news.aspx")

XMLTextObject.WriteElementString("description", "This is a sample RSS 2.0 Feed")

XMLTextObject.WriteElementString("copyright", "(c) 2006, All rights reserved.")

XMLTextObject.WriteElementString("ttl", "5")

Dim connectionstring As String = "Data Source=.\sqlexpress;Initial

Catalog=rsssample;Integrated Security=True"

Dim objConnection As New SqlConnection(connectionstring)

objConnection.Open()

Dim sql As String = "SELECT TOP 3 newstitle, newsdescription, newsdate FROM

newsdata ORDER BY newsdate DESC"

Dim objCommand As New SqlCommand(sql, objConnection)

 

Dim objReader As SqlDataReader = objCommand.ExecuteReader()

Do While objReader.Read()

XMLTextObject.WriteStartElement("item")

XMLTextObject.WriteElementString("title", objReader.GetString(0))

XMLTextObject.WriteElementString("description", objReader.GetString(1))

XMLTextObject.WriteElementString("link",

"http://www.somedomain.com/GetArticle.aspx?id=0")

XMLTextObject.WriteElementString("pubDate",

objReader.GetDateTime(2).ToString("R"))

XMLTextObject.WriteEndElement()

Loop

objReader.Close()

objConnection.Close()

XMLTextObject.WriteEndElement()

XMLTextObject.WriteEndElement()

XMLTextObject.WriteEndDocument()

XMLTextObject.Flush()

XMLTextObject.Close()

Response.End()

Catch ex As Exception

Label1.Text = ex.Message

End Try

End Sub

End Class

 

 

 

In C#

using System;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;

using System.Text;

using System.Xml;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

try

{

Response.Clear();

Response.ContentType = "text/xml";

XmlTextWriter XMLTextObject = new XmlTextWriter(Response.OutputStream,

Encoding.UTF8);

XMLTextObject.WriteStartDocument();

XMLTextObject.WriteStartElement("rss");

XMLTextObject.WriteAttributeString("version", "2.0");

XMLTextObject.WriteStartElement("channel");

XMLTextObject.WriteElementString("title", "Sample News Channel");

XMLTextObject.WriteElementString("link",

"http://www.somedomain.com/news.aspx");

 

Implementing RSS and Atom Feed

 

XMLTextObject.WriteElementString("description", "This is a sample RSS 2.0

Feed");

XMLTextObject.WriteElementString("copyright", "(c) 2006, All rights

reserved.");

XMLTextObject.WriteElementString("ttl", "5");

string connectionstring = "Data Source=.\\sqlexpress;Initial

Catalog=rsssample;Integrated Security=True";

SqlConnection objConnection = new SqlConnection(connectionstring);

objConnection.Open();

string sql = "SELECT TOP 3 newstitle, newsdescription, newsdate FROM

newsdata ORDER BY newsdate DESC";

SqlCommand objCommand = new SqlCommand(sql, objConnection);

SqlDataReader objReader = objCommand.ExecuteReader();

while (objReader.Read())

{

XMLTextObject.WriteStartElement("item");

XMLTextObject.WriteElementString("title",

objReader.GetString(0));

XMLTextObject.WriteElementString("description",

objReader.GetString(1));

XMLTextObject.WriteElementString("link",

"http://www.somedomain.com/GetArticle.aspx?id=0");

XMLTextObject.WriteElementString("pubDate",

objReader.GetDateTime(2).ToString("R"));

XMLTextObject.WriteEndElement();

}

objReader.Close();

objConnection.Close();

XMLTextObject.WriteEndElement();

XMLTextObject.WriteEndElement();

XMLTextObject.WriteEndDocument();

XMLTextObject.Flush();

XMLTextObject.Close();

Response.End();

}

catch (Exception ex)

{

Label1.Text = ex.Message;

}

}

}

6. In Listing B.3, the following syntax is used to generate a query to retrieve the top three rows of data from the

newsdata table:

string sql = "SELECT TOP 3 newstitle, newsdescription, newsdate FROM newsdata ORDER BY newsdate DESC";

In case of the CreateRSSFeedVB website, we have also added the following connection string in the web.config file of the website:

 

Source=.\\sqlexpress;Initial Catalog=rsssample;Integrated Security=True"/>

7. Now, press the F5 key to run the CreateRSSFeed website. The output is shown in

 

Figure B.1: Output of the CreateRSSFeed Example

The data displayed on the webpage is based on the data in the newsdata table

Consuming an RSS Feed

Numerous websites and blogs share their content through RSS feeds over the Internet. Now, let s understand

how a website consumes an RSS feed.

Perform the following steps to see how a website can consume an RSS feed:

1. Create a website and name it ConsumeRSSFeed. You can find this website as ConsumeRSSFeed (in C#) and

ConsumeRSSFeedVB (in VB) in the CD-ROM.

2. Next, replace the code for the Default.aspx file with the following code to add the controls required for

the ConsumeRSSFeed website. You can find the code for the Default.aspx page in

Showing the Code for the Default.aspx Page

In VB

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"

Inherits="_Default" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Consuming RSS Feed

Implementing RSS and Atom Feed

 


Text="CNN.com RSS Feed">


DataSourceID="XmlDataSource1" BackColor="White"

BorderColor="#999999" BorderStyle="None"

BorderWidth="1px" CellPadding="3"

GridLines="Vertical"

Font-Size="XX-Small" Width="308px">

<%#XPath("title")%>

<%#XPath("description")%>

<%#XPath("link")%>

ForeColor="Black" />

Bold="True" ForeColor="White" />

ForeColor="White" />


DataFile="http://rss.cnn.com/rss/cnn_topstories.rss"

XPath="rss/channel/item">

 

In C#

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"

Inherits="_Default" %>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Consuming RSS Feed


Feed">


BackColor="White"

BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"

GridLines="Vertical"

Font-Size="XX-Small" Width="308px">

<%#XPath("title")%>

<%#XPath("description")%>

<%#XPath("link")%>


DataFile="http://rss.cnn.com/rss/cnn_topstories.rss"

XPath="rss/channel/item">

 

3. Next, specify the path of the RSS feed in the Data file text box in the Configure Data Source dialog box while configuring the data source to display the data in the ConsumeRSSFeed website, as shown in Figure B.2:

Implementing RSS and Atom Feed

 

4. Similarly, in the Transform file text box of the Configure Data Source dialog box, you can provide the

path of the .xls file (Figure B.2). With XSLT, you can transform an XML document into HTML to help you to provide a different structure to an XML file.

5. In the XPath expression text box, specify an optional XPath expression that allows you to filter the data in the XML file to return a subset of the file (Figure B.2).

6. Now, press the F5 key to run the ConsumeRSSFeed website. The output is shown in Figure B.3:

Figure B.3: Output of the ConsumeRssFeed Website

To summarize, you have learned about the Web feeds RSS and Atom. You have also learned about the features, syntax, and various versions of RSS and Atom feeds and how to create an RSS feed and how can it be consumed through a website.

Note: This article is created with the help of "Black Book"

 

Thanks !!!

 

 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend