Rss
This is actually not complicated if you want to use the. NET Framework for RSS feeds. All you have to do is take the following steps:
Link to a Web site that provides RSS feeds
Download Feed XML
To load the XML of the feed into an object that is allowed to search
Search the XML for the feed for the node you want to extract
The. NET Framework provides built-in functions to complete all tasks. All we have to do is bind these features together so that we can use RSS feeds.
Link to server
We can use the WebRequest object to link to the server. WebRequest objects allow you to post requests on a Web site, and since RSS is transmitted over HTTP, the WebRequest object becomes the primary choice for the linked server.
The code in Listing a tells us any connection to a new WebRequest object with a URL.
Listing A
Create a WebRequest object
WebRequest myrequest = webrequest.create (URL);
In this example, you can also replace the "url" in the RSS feed with a full URL. The following is the address of the MSN Automotive RSS feed: http://rss-feeds.msn.com/autos/autosnews.xml
Download RSS Data
When we connect to the server, we need to download the data provided by the feed. The WebRequest object provides a GetResponse () method for achieving this goal. The Webrequest.getresponse () method returns a WebRequest object that gives us a response to the server according to our request.
Here we will use the GetResponseStream () method of the WebResponse (Web Response) object. This method returns a Stream object that contains the original RSS XML that the server responds to. The code in Listing B tells us how to get the WebResponse (Web Response) object from the WebRequest (Web request) object and how to respond to the stream from the WebResponse (Web Response) object.
Listing B
Get the response from the WebRequest
WebResponse myresponse = Myrequest.getresponse ();
Get the response ' s stream
Stream Rssstream = Myresponse.getresponsestream ();
Loading RSS data into an XML document
Once we get the stream from the WebResponse (Web Response) object, we load this down into the XmlDocument object. This makes it easy to analyze the XML data and get the value from it easily. The easiest way to get the XmlDocument load stream is to create a new XmlDocument object and pass our stream to the Load method. Listing C has explained the use of this method for us.
Listing C
Create the Xml Document
XmlDocument document = Newxmldocument ();
The Load the stream into the XmlDocument object.
Document. Load (Rssstream);
Parsing xml
This is the hardest part of using the RSS feed. We have to use the XmlDocument we just created to get the XML nodes that contain our own data. The nodal points of our general interest are:
The title of the feed, which is stored in the/rss/channel/title file in the feed XML.
The feed article, which is stored in the/rss/channel/item file in the feed XML. There may be multiple nodes in this position.
The title of the article, which is stored in the title of the article node.
The description of the article, which is stored in the description of the article node.
Article link, it is stored in the article node link inside.
We can get these nodes using the selectSingleNode function and the SelectNodes function built into the XmlDocument object. Both of these functions can accept XPath queries, and you can return one or more nodes that match the results of the query.
Listing d This code tells us how to use XmlDocument and XPath to parse out each individual element from the RSS feed.
Listing D
Get a XmlDocument object that contains the feed ' s XML
XmlDocument feeddocument =
Getxmldocumentfromfeed ("Http://rss-feeds.msn.com/autos/autosnews.xml");
Create a XmlNamespaceManager for our namespace.
XmlNamespaceManager Manager =
Newxmlnamespacemanager (feeddocument.nametable);
Add the RSS namespace to the manager.
Manager. AddNamespace ("RSS", "http://purl.org/rss/1.0/");
Get the title node out of the RSS document
XmlNode Titlenode =
Feeddocument.selectsinglenode ("/rss/channel/title", manager);
Get the article Nodes
XmlNodeList Articlenodes =
Feeddocument.selectnodes ("/rss/channel/item", manager);
Loop through the articles and extract
Their data.
foreach (XmlNode articlenode in Articlenodes)
{
Get the article ' s title.
string title =
Articlenode.selectsinglenode ("title", manager). InnerText;
Get the article ' s link
String link =
Articlenode.selectsinglenode ("link", manager). InnerText;
Get the article ' s description
String Description =
Articlenode.selectsinglenode ("description", manager). InnerText;
}
Not all RSS feeds are created the same
If all RSS feeds are in the same format, it will become more powerful, but there are many different versions and implementations of RSS feeds. The format described in this article is suitable for most feeds, and there may be a small portion of the RSS feed format that is different from this format.