Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old February 22nd, 2006, 12:35 AM
blackstaronline.net
Guest
 
Posts: n/a
Default Returning only X number of items...

Here is my working code to pull Yahoo business news RSS feed. Can
anyone show me how to only return the top 3 or 4 news articles?

<%@ Import Namespace="System.Xml" %>
<script language="VB" runat="server">
Public Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
If Cache("RSS1") Is Nothing then
Dim dt as DataTable =
GetRSSFeed("http://rss.news.yahoo.com/rss/business")
Cache.Insert("RSS1", dt, Nothing, DateTime.Now.AddMinutes(180),
TimeSpan.Zero)
End If

rss.DataSource = Cache("RSS1")
rss.DataBind()
End Sub

Function GetRSSFeed(strURL as String) as DataTable
Dim reader as XmlTextReader = New XmlTextReader(strURL)

Dim ds as DataSet = New DataSet()
ds.ReadXml(reader)
Return ds.Tables(3)
End Function
</script>


<asp:DataList runat="server" id="rss">
<itemtemplate>
<font size="2" face="Geneva, Arial, Helvetica, san-serif"><b><a
href="<%# DataBinder.Eval(Container.DataItem, "link") %>"
target="_blank"><%# DataBinder.Eval(Container.DataItem, "title")
%></a></b><br />
<%# DataBinder.Eval(Container.DataItem, "description")
%></font><br />
</itemtemplate>
</asp:DataList>


Thanks in advance,
Jeremy Reid
http://hgtit.com

  #2  
Old February 22nd, 2006, 12:45 PM
dickster
Guest
 
Posts: n/a
Default Re: Returning only X number of items...

Jeremy

This is not a cut and paste answer as I have DataBound my datatable to
a Datagrid (called rss) for ease. I've also skipped out the Cache
stuff.

Should be simple for you to convert to your DataList & use the Cache

================================================== ===
You could use DataView, filter it and then DataBind to the DataView
I notice that there is a Item_Id column in the DataTable returned from
GetRSSFeed()

Public Sub Page_Load(ByVal sender As System.Object, _
ByVal e As
System.EventArgs)

Dim dv As New DataView
Dim dt as DataTable = _

GetRSSFeed("http://rss.news.yahoo.com/rss/business")

dv = dt.DefaultView
dv.RowFilter = "Item_Id <3"

rss.DataSource = dv
rss.DataBind()

End Sub

  #3  
Old February 22nd, 2006, 02:55 PM
dickster
Guest
 
Posts: n/a
Default Re: Returning only X number of items...

Jeremy

This is not a cut and paste answer.

In working out a solution I have DataBound the datatable to a Datagrid
(called rss).

I've also skipped out the Cache stuff.

Should be simple for you to convert to your DataList & use the Cache

================================================== ===
My solution is as follows, use a DataView, filter it and then DataBind
the DataView to the DataGrid (in your instance the DataList)

I notice that there is a Item_Id column in the DataTable returned from
GetRSSFeed() i used this value in the filter to get the first 3

Here's the code:

Public Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs)

Dim dv As New DataView
Dim dt as DataTable = _

GetRSSFeed("http://rss.news.yahoo.com/rss/business")

dv = dt.DefaultView
dv.RowFilter = "Item_Id <3"

rss.DataSource = dv
rss.DataBind()

End Sub

  #4  
Old February 22nd, 2006, 03:45 PM
dickster
Guest
 
Posts: n/a
Default Re: Returning only X number of items...

Question:

In this instance why does the <guid isPermaLink="false"> node in each
of the item nodes of the RSS feed appear to become a index field called
"Item_Id" in the DataTable.

Is this a convention?

  #5  
Old February 22nd, 2006, 04:35 PM
blackstaronline.net
Guest
 
Posts: n/a
Default Re: Returning only X number of items...

Thanks a lot man, that worked great! I ended taking what you gave and
still using the cache because theres too munch of a lag when it has to
go to yahoo and pull the feed every time a page is requested. This way
it caches it locally for 3 hours. Thanks again, this is what I have
now.


If Cache("RSS2") Is Nothing then
Dim dv As New DataView
Dim dt as DataTable =
GetRSSFeed("http://rss.news.yahoo.com/rss/business")
dv = dt.DefaultView
dv.RowFilter = "Item_Id < 4"
Cache.Insert("RSS2", dv, Nothing, DateTime.Now.AddMinutes(180),
TimeSpan.Zero)
End if
rss.DataSource = Cache("RSS2")
rss.DataBind()
End Sub


Function GetRSSFeed(strURL as String) as DataTable
'Get the XML data
Dim reader as XmlTextReader = New XmlTextReader(strURL)

'return a new DataSet
Dim ds as DataSet = New DataSet()
ds.ReadXml(reader)
Return ds.Tables(3)
End Function

It works great,

Thanks again.

Jeremy Reid
http://hgtit.com

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles