XML has been around for a while but is starting to really pick up with the use of RSS feeds growing. Many companies have news, blogs, and much more available through custom RSS feeds hosted on their site. The great part about them is that they are responsible for updating them. What this does is allow a "smaller" website to have late breaking news articles on his/her site without lifting a finger. It provides a way for webmasters to have ever changing dynamic content on his/her site without the hastle of spending hours a day writing. Today we are going to build a .NET script that pulls the XML RSS feed from the source, reads it and displays its content on your page. Once that's done I'll show you a couple tricks to improve performance and appearance.
First let me show you what the bulk of the script will look like. Then I'll go through piece by piece and explain what its doing. For this example I am going to use the Yahoo World News RSS Feed. Now keep in mind, you will need to read up on each RSS feed you are pulling to ensure you are complying with their terms of use. You are dealing with content that belongs to someone else so be sure you know all laws and their rules when it comes to the content you are pulling. Generally speaking if you are not directly making money using their feeds then you’re OK. But its your responsibility to know what you can and can't do with a 3rd party RSS Feed.
Here we go.
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
dlRSSData.DataSource = ReadRSS("http://rss.news
.yahoo.com/rss/world")
dlRSSData.DataBind()
End Sub
Function ReadRSS(strRSSURL as String) as DataTable
Dim XMLreader as XmlTextReader = New
XmlTextReader(strRSSURL)
Dim objds as DataSet = New DataSet()
objds.ReadXml(XMLreader)
Return ds.Tables(3)
End Function
</script>
<html>
<body>
<form runat="server">
<asp:DataList runat="server" id="dlRSSData">
<itemtemplate>
<a href="<%# DataBinder.Eval(Container.DataItem, "Link")
%> " target="_blank">
<%# DataBinder.Eval(Container.DataItem, "title") %> </a> <br />
<%# DataBinder.Eval(Container.DataItem, "description") %>
<br /> <br />
</itemtemplate>
</asp:DataList>
</form>
</body>
</html>
Alright, that believe it or not is ALL the code for the page. Its starts off by importing the system.data and system.XML namespaces. This lets the .NET server know that you plan on using both a form of data and XML (in this case its the same document) in your code. It tells the .NET server all it needs to know regarding the commands you'll pass to it like 'Dim XMLreader as XmlTextReader'.
In the first line of our Page_Load script we set the datasource of our datalist. If you look in the HTML section of the code we have a datalist on the page. I'll explain more about that in a little bit. Anyway, in the first line we set the datasource equal to the results of a function. Pretty cool programming going on here. We are calling a function and passing it the location of the RSS feed. When the function gets done running it will return some data and that data will then be the datasource to our DataList. Make sense? Let’s look further. If we follow the code down to the function that was called we see are creating a new XMLTextReader. We do this so that when we pass it the RSS XML doc it will know how to understand it. We then create a dataset off that XML data and we tell it to return the data to the Sub that called the function. In this case its the datasource code for our datalist. We then immedietly bind the data to the datalist and were done. The page will load.
So the series of events that happen are;
1. Page is requested on the .NET server2. .Net creates the datasource for the datalist by running a function called 'ReadRSS' passing it the actual location of the RSS feed.3. The function creates a XML reader and returns it to the datalist4. The datalist is bound to the XML data5. The page displays the formatted XML data on the page.Pretty simple and easy with BIG results.
Now before we began I promised you that I'd show you how to also improve performance and appearance. So here we go. If you look at the code it’s having to go to yahoo's site and pull the XML data every time the page is requested. This can cause your site to slow significantly not only for 1 hit but especially if your dealing with thousands of hits. One method to resolve this is to "cache" the XML data on your site and pull from that cache. You can set a time limit on the cache so that you get the latest data release every X number of minutes. That way you are still pulling dynamic up to date content but with a significantly less amount of overhead in bandwidth and time.
If you reference the above code our first line in the <script> tag is setting the data source to the datalist. We are going to add some cache code before it to see if we have a valid cache of the feed, if we do then we use the cached data if we don't then it will go to yahoo's site and get the latest RSS feed. Then we pick up where we started above but instead of setting the datasource from the live data we set it from the cache data. This will speed up your performance drastically. As a test try writing your code the non-cached way. Test it out and get a feel for how long it takes to pull and display the data. Then immediately change the code to the cached version and see the difference.
The cached way of doing it looks like this.
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<script language="VB" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If Cache("RSSfeed") Is Nothing then
'Item not in cache, go and get it
Dim dt as DataTable = GetRSSFeed("http://rss.news
.yahoo.com/rss/world")
Cache.Insert("RSSfeed", dt, Nothing, DateTime.
Now.AddMinutes(60), TimeSpan.Zero)
End If
dlRSSData.DataSource = Cache("RSSfeed")
dlRSSData.DataBind()
End Sub
Function ReadRSS(strRSSURL as String) as DataTable
Dim XMLreader as XmlTextReader = New
XmlTextReader(strRSSURL)
Dim objds as DataSet = New DataSet()
objds.ReadXml(XMLreader)
Return ds.Tables(3)
End Function
</Script>
<html>
<body>
<form runat="server">
<asp:DataList runat="server" id="dlRSSData">
<itemtemplate>
<a href="<%# DataBinder.Eval(Container.DataItem, "Link")
%> " target="_blank">
<%# DataBinder.Eval(Container.DataItem, "title") %> </a> <br />
<%# DataBinder.Eval(Container.DataItem, "description")
%> <br /> <br />
</itemtemplate>
</asp:DataList>
</form>
</body>
</html>
Now its almost doing the same thing as above except it is checking for a cached version of the feed. It there is one then it uses that, if not then it creates a cached version by going and getting it and running the function to read it. Then we set our datasource for our datalist from the cache. Its simple and quick.
Now we can easily change the appearance of our data by adding appearance code to our datalist object in the HTML part of the page. Here is an example on how to change the font and color of the text to the feed.
<asp:DataList runat="server" id="dlRSSData">
<itemtemplate>
<font size="2" face="Arial" color="#ff000">
<a href="<%# DataBinder.Eval(Container.DataItem, "Link")
%> " target="_blank">
<%# DataBinder.Eval(Container.DataItem, "title") %> </a> <br />
<%# DataBinder.Eval(Container.DataItem, "description")
%> </font> <br /> <br />
</itemtemplate>
</asp:DataList>
Now with 2 little snippets of code we changed the font size, face, and color. Its easy and makes the XML feed look great.
Labels: ASP, ASP.NET, Custom, Feed, HTML, Reader, RSS, VB