all groups > dotnet xml > february 2006 >
You're in the

dotnet xml

group:

Returning only X number of items...



Returning only X number of items... blackstaronline.net
2/21/2006 4:24:06 PM
dotnet xml: 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
Re: Returning only X number of items... dickster
2/22/2006 4:33:25 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
Re: Returning only X number of items... dickster
2/22/2006 6:39:46 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
Re: Returning only X number of items... dickster
2/22/2006 7:37:22 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?
Re: Returning only X number of items... blackstaronline.net
2/22/2006 8:23:18 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
AddThis Social Bookmark Button