HighTechTalks DotNet Forums  

Returning only X number of items...

Dotnet XML microsoft.public.dotnet.xml


Discuss Returning only X number of items... in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
blackstaronline.net
 
Posts: n/a

Default Returning only X number of items... - 02-21-2006 , 06:24 PM






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>


<aspataList 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>
</aspataList>


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


Reply With Quote
  #2  
Old   
dickster
 
Posts: n/a

Default Re: Returning only X number of items... - 02-22-2006 , 06:33 AM






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


Reply With Quote
  #3  
Old   
dickster
 
Posts: n/a

Default Re: Returning only X number of items... - 02-22-2006 , 08:39 AM



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


Reply With Quote
  #4  
Old   
dickster
 
Posts: n/a

Default Re: Returning only X number of items... - 02-22-2006 , 09:37 AM



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?


Reply With Quote
  #5  
Old   
blackstaronline.net
 
Posts: n/a

Default Re: Returning only X number of items... - 02-22-2006 , 10:23 AM



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


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.