Assimilate XML RSS Feed from URL in C#

Published Wed, Apr 14 2004 21:46 | coad

This piece of code pulls an RSS feed and displays the title of the articles.  It is here to demonstrate just how easy it is to pull XML off the web and utilize the data.

 

I use Trace.WriteLine instead of Console.WriteLine so the report is fed to the Visual Studio output window and can be run as either a Console or Windows app, but at least a new Console window is not opened.

 

 

using System.Xml;

using System.Net;

using System.Text;

using System.Diagnostics;

 

// -------------------------------

 

// RSS XML Feed URL

string url = "http://msmvps.com/coad/rss.aspx";

 

// Create an interface to the web

WebClient c = new WebClient();

 

// Download the XML into a string

string xml = ASCIIEncoding.Default.GetString(c.DownloadData(url));

 

// Document to contain the feed

XmlDocument doc = new XmlDocument();

 

// Parse the xml

doc.LoadXml(xml);

 

// Display each article title

foreach (XmlNode node in doc.SelectNodes("/rss/channel/item/title"))

   Trace.WriteLine(node.InnerText);

 

 

Filed under: ,

Comments

# TrackBack said on April 15, 2004 11:16 AM:

# coad said on January 14, 2005 5:16 PM:

i am just starting.. i put this code into a .cs file and it did not work.
hmm

# coad said on January 14, 2005 9:57 PM:

That code is the heart of what is required. It takes putting it in the right place to "get it to work". Here is the code that will execute that above verbatim. You can put this in a .cs file, compile that file, and execute it from the command prompt to see the results.

Unforunately, when I post this, the blog removes the indentation. Please know that they are in here for good form. :)

The TraceToConsole class is added to the end to pass the Trace output from the example code above staight to the console. This way, if you had a GUI trace listening attached (like Visual Studio), you'll see the output there too (in VS' Output window). You can change the last line above from "Trace." to "Console." to send the output to the console directly.

=========================================

using System;
using System.Xml;
using System.Net;
using System.Text;
using System.Diagnostics;

namespace RullRSSFeed4Blog
{
class QuickTest
{
[STAThread]
public static void Main(string[] args)
{
Trace.Listeners.Add(new TraceToConsole());
Trace.Write("\n");
ExampleCode();
}

private static void ExampleCode()
{
// RSS XML Feed URL
string url = "http://msmvps.com/coad/rss.aspx";

// Create an interface to the web
WebClient c = new WebClient();

// Download the XML into a string
string xml = ASCIIEncoding.Default.GetString(c.DownloadData(url));

// Document to contain the feed
XmlDocument doc = new XmlDocument();

// Parse the xml
doc.LoadXml(xml);

// Display each article title
foreach (XmlNode node in doc.SelectNodes("/rss/channel/item/title"))
Trace.WriteLine(node.InnerText);
}
}

class TraceToConsole : TraceListener
{
public override void Write(string s)
{ Console.Write(s); }

public override void WriteLine(string s)
{ Console.WriteLine(s); }
}
}

# coad said on January 15, 2005 2:19 AM:

Thanks SOO much.

--mark

# Limber said on January 18, 2006 10:59 AM:

Are you aware of that this method locks the GUI, if you have one, while fetching remote data?

# coad said on February 21, 2007 1:43 PM:

Limber,

Yes, it is a syncronous operation.  For my applications (in this case a command line app) I want it to be syncronous.  If it's in a GUI app, it'll delay responsiveness. It is easy to turn it into an asyncronous procedure by using the BackgroundWorker Control, an asyncronous delegate, or the Begin/End async methods of the key calls.

# Noah Coad's Code said on February 23, 2007 12:28 PM:

I was digging around recently and came across an interesting fact... My blog gets more views per month

# Martin Lee said on August 10, 2007 9:10 PM:

Become a more intelligent investor. Learn the value investing strategies of Warren Buffett as well as that of his mentor, Benjamin Graham.

# Martin Lee said on October 15, 2007 9:44 PM:

Learn Strategies and Tips for Making Money Online Using Affiliate Marketing.

# K Naib said on October 26, 2007 3:43 PM:

Information, history, buying advice and industry news on various famous watch brands including Accurist, Rotary, Citizen, Tissot, Sekonda, Police, Guess and more.

# Jason Green said on November 27, 2007 1:15 AM:

If you become a subscriber to Site Salary you will also be presented with exclusive tips and advice on monetizing your website.