Click here to Skip to main content
15,886,026 members
Articles / Programming Languages / C#

Simple RSS

Rate me:
Please Sign up or sign in to vote.
3.65/5 (13 votes)
15 Jan 2006CPOL2 min read 64.9K   942   48  
Build your own RSS reader with a couple of easy-to-use classes!
using System;
using System.Xml;

namespace RavSoft
{
	/// <summary>
	/// An item contained in an RSS feed.
	/// </summary>
	public class RSSItem
	{
    /////////////
    // Operations

    /// <summary>
    /// Creates an RSSItem from an element in an channel feed.
    /// </summary>
    /// <param name="element">Element in channel feed.</param>
    /// <returns>Created RSSItem or null if an error occured.</returns>
    public static RSSItem createFromElement
      (XmlElement element)
    {
      // Get item title - fail if missing
      XmlNodeList elements = element.GetElementsByTagName ("title");
      if (elements.Count == 0)
         return (null);
      XmlElement ele = elements.Item (0) as XmlElement;
      string strTitle = ele.InnerText;
      strTitle = StringParser.removeHtml (strTitle);

      // Get item link - fail if missing
      elements = element.GetElementsByTagName ("link");
      if (elements.Count == 0)
         return (null);
      ele = elements.Item (0) as XmlElement;
      string strLink = ele.InnerText;
      strLink = strLink.Replace ("<![CDATA[", "");
      strLink = strLink.Replace ("]]>", "");
      strLink = strLink.Trim();

      // Get optional author
      string strAuthor = "";
      elements = element.GetElementsByTagName ("author");
      if (elements.Count != 0) {
          ele = elements.Item (0) as XmlElement;
          strAuthor = ele.InnerText.Trim();
      }

      // Get optional description
      string strDesc = "";
      elements = element.GetElementsByTagName ("description");
      if (elements.Count != 0) {
          ele = elements.Item (0) as XmlElement;
          strDesc = ele.InnerText.Trim();
      }
      strDesc = StringParser.removeHtml (strDesc);

      // Get optional publication timestamp
      string strPublished = "";
      elements = element.GetElementsByTagName ("pubDate");
      if (elements.Count != 0) {
          ele = elements.Item (0) as XmlElement;
          strPublished = ele.InnerText;

          // Remove trailing GMT offset if present
          if (strPublished.Length > 6) {
              char[] chPublished = strPublished.ToCharArray();
              char ch = chPublished[ strPublished.Length - 5];
              if ((ch == '-') || (ch == '+'))
                 strPublished = strPublished.Remove (strPublished.Length - 5, 5);
          }
          strPublished = strPublished.Trim();
      }

      // Return item
      RSSItem rssItem = new RSSItem();
      rssItem.m_strTitle = strTitle;
      rssItem.m_strLink = strLink;
      rssItem.m_strAuthor = strAuthor;
      rssItem.m_strDescription = strDesc;
      rssItem.m_strPublished = strPublished;
      return (rssItem);
    }

    /////////////
    // Properties

    /// <summary>
    /// The item's title.
    /// </summary>
    public string Title {
      get {
        return m_strTitle;
      }
    }

    /// <summary>
    /// The item's description.
    /// </summary>
    public string Description {
      get {
        return m_strDescription;
      }
    }

    /// <summary>
    /// The item's link.
    /// </summary>
    public string Link {
      get {
        return m_strLink;
      }
    }

    /// <summary>
    /// The item's author.
    /// </summary>
    public string Author {
      get {
        return m_strAuthor;
      }
    }

    /// <summary>
    /// The item's publication timestamp.
    /// </summary>
    public DateTime Published {
      get {
        DateTime tmPublished = DateTime.MinValue;
        try {
          tmPublished = DateTime.Parse (m_strPublished);
        } catch (Exception) {
          // Nothing to do
        }
        return tmPublished;
      }
    }

    /// <summary>
    /// The item's publication timestamp as a string.
    /// </summary>
    public string PublishedAsString {
      get {
        return m_strPublished;
      }
    }

    /////////////
    // Attributes

    /// <summary>Item title.</summary>
    string m_strTitle = "";

    /// <summary>Link to item.</summary>
    string m_strLink = "";

    /// <summary>Item author.</summary>
    string m_strAuthor = "";

    /// <summary>Item description.</summary>
    string m_strDescription = "";

    /// <summary>Publication timestamp.</summary>
    string m_strPublished = "";
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Canada Canada
Ravi Bhavnani is an ardent fan of Microsoft technologies who loves building Windows apps, especially PIMs, system utilities, and things that go bump on the Internet. During his career, Ravi has developed expert systems, desktop imaging apps, marketing automation software, EDA tools, a platform to help people find, analyze and understand information, trading software for institutional investors and advanced data visualization solutions. He currently works for a company that provides enterprise workforce management solutions to large clients.

His interests include the .NET framework, reasoning systems, financial analysis and algorithmic trading, NLP, HCI and UI design. Ravi holds a BS in Physics and Math and an MS in Computer Science and was a Microsoft MVP (C++ and C# in 2006 and 2007). He is also the co-inventor of 3 patents on software security and generating data visualization dashboards. His claim to fame is that he crafted CodeProject's "joke" forum post icon.

Ravi's biggest fear is that one day he might actually get a life, although the chances of that happening seem extremely remote.

Comments and Discussions