A simple way to read an RSS feed using C#

by Heathesh 10. May 2010 22:26

I found a decent RSS news feed that was free (http://freenewsfeed.newsfactor.com/rss) and was looking at integrating it into my new blog design. In order to do this I wanted to create a simple class that would read an RSS feed based on a URL passed in, and I thought the best way to do this would be to first create a class I could use to store the RSS feed items. So I created the following:

    /// <summary>
    /// RSS feed item entity
    /// </summary>
    public class RssFeedItem
    {
        /// <summary>
        /// Gets or sets the title
        /// </summary>
        public string Title { get; set; }
       
        /// <summary>
        /// Gets or sets the description
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets the link
        /// </summary>
        public string Link { get; set; }

        /// <summary>
        /// Gets or sets the item id
        /// </summary>
        public int ItemId { get; set; }

        /// <summary>
        /// Gets or sets the publish date
        /// </summary>
        public DateTime PublishDate { get; set; }

        /// <summary>
        /// Gets or sets the channel id
        /// </summary>
        public int ChannelId { get; set; }
    }


Next I needed a manager class that would read any RSS feed and populate a list of the above rss feed item object with the relevant data. To achieve this I created the following static class:

    /// <summary>
    /// RSS manager to read rss feeds
    /// </summary>
    public static class RssManager
    {
        /// <summary>
        /// Reads the relevant Rss feed and returns a list off RssFeedItems
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static List<RssFeedItem> ReadFeed(string url)
        {
            //create a new list of the rss feed items to return
            List<RssFeedItem> rssFeedItems = new List<RssFeedItem>();

            //create an http request which will be used to retrieve the rss feed
            HttpWebRequest rssFeed = (HttpWebRequest)WebRequest.Create(url);

            //use a dataset to retrieve the rss feed
            using (DataSet rssData = new DataSet())
            {
                //read the xml from the stream of the web request
                rssData.ReadXml(rssFeed.GetResponse().GetResponseStream());

                //loop through the rss items in the dataset and populate the list of rss feed items
                foreach (DataRow dataRow in rssData.Tables["item"].Rows)
                {
                    rssFeedItems.Add(new RssFeedItem
                    {
                        ChannelId = Convert.ToInt32(dataRow["channel_Id"]),
                        Description = Convert.ToString(dataRow["description"]),
                        ItemId = Convert.ToInt32(dataRow["item_Id"]),
                        Link = Convert.ToString(dataRow["link"]),
                        PublishDate = Convert.ToDateTime(dataRow["pubDate"]),
                        Title = Convert.ToString(dataRow["title"])
                    });
                }
            }

            //return the rss feed items
            return rssFeedItems;
        }
    }


I was all set, all I had to do now was pass in a URL of an RSS feed, and I would be returned a list of rss feed items...

Happy rss-ing!

Tags: , , ,

Development | .Net | RSS

How to execute a method on a static class using reflection

by Heathesh 1. December 2009 22:49

First, create a method like so:

    /// <summary>
    /// Execute a method on an assembly's static class
    /// </summary>
    /// <param name="assemblyNamespace">The namespace of the assembly you would like to invoke</param>
    /// <param name="className">The name of the class in the assembly you would like to invoke</param>
    /// <param name="methodToExecute">The name of the method in the class to invoke</param>
    /// <param name="parameters">array of objects to be passed into the method we're invoking</param>
    /// <returns>object of whatever the method invoked returns</returns>
    public static object ExecuteMethod(string assemblyNamespace, string className, string methodToExecute,
        params object[] parameters)
    {
        Assembly assembly = Assembly.Load(assemblyNamespace);
        Type classType = assembly.GetType(string.Format("{0}.{1}", assemblyNamespace, className));
        MethodInfo methodInfo = classType.GetMethod(methodToExecute);
        return methodInfo.Invoke(null, parameters);
    }

Make sure you add the relevant using:

    using System.Reflection;

Now we need to call the method. The method I want to invoke is in this class:

    namespace Demo.BusinessLogic
    {
        /// <summary>
        /// User manager class to create, read, update and delete users
        /// </summary>
        public static class UserManager
        {
            /// <summary>
            /// Method to retrieve a User
            /// </summary>
            /// <param name="userId"></param>
            /// <returns></returns>
            public static User Read(int userId)
            {
                User user = new User();

                //TODO add code to retrieve user for Id here

                return user;
            }
        }
    }


To call the method and execute Read the user id 12, I'm going to simply do this:

    object returnedObject = ExecuteMethod("Demo.BusinessLogic", "UserManager", "Read", 12);

And returnedObject comes back populated with my user object!

Tags: , , , ,

Development | .Net



Powered by BlogEngine.NET 1.5.0.7 (with enhancements by Heathesh)
Theme by Mads Kristensen (with tweeks by Heathesh)

Certifications

Microsoft Certified Professional

Microsoft Certified Technology Specialist

Answer Questions

 

Tag cloud

Calendar

<<  July 2014  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

http://heathesh.com