Returning top X number of items...

  • Thread starter Thread starter blackstaronline.net
  • Start date Start date
B

blackstaronline.net

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
 
Jeremy,
Just iterate over the rows in your DataTable and call the DataRow.Delete()
method
on as many as you want.
Then call AcceptChanges on the table.
Peter
 
Thanks for your help Peter. What I ended up doing was calling the
Row.Filter and it seems to works great. Can you think of a reason why
DataRow.Delete() is a better suited fix? If so I'll use it but this
seems to work great.



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


Thanks again for your help Peter,
Jeremy Reid
http://hgtit.com
 
Back
Top