Click here to Skip to main content
15,881,882 members
Articles / Web Development / ASP.NET

A very standard, powerful and easy to use Sample and Library for working on RSS 2.0

Rate me:
Please Sign up or sign in to vote.
4.85/5 (23 votes)
18 Feb 2005MIT1 min read 46K   649   51  
With this library, you can easily create some RSS 2.0 files for your site and/or use some RSS 2.0 files from other sites into your site.
namespace IranianExperts.RSS
{
	public sealed class RSSUtilities
	{
		private RSSUtilities(){}

		/// <summary>
		/// Get some DataTable from RSS file.
		/// </summary>
		/// <param name="location"></param>
		/// <param name="address"></param>
		/// <param name="feedType"></param>
		/// <returns></returns>
		public static System.Data.DataTable GetRSSFeed(RSSLocation location, string address, RSSFeedType feedType)
		{
			int intIndex = 0;
			int intItemTableIndex = -1;

			System.IO.StreamReader oStreamReader = null;
			System.Xml.XmlTextReader oXmlTextReader = null;

			switch(location)
			{
				case RSSLocation.URL:
					oXmlTextReader = new System.Xml.XmlTextReader(address);
					break;

				case RSSLocation.Drive:
					oStreamReader = new System.IO.StreamReader(address, System.Text.Encoding.UTF8);
					oXmlTextReader = new System.Xml.XmlTextReader(oStreamReader);
					break;
			}

			System.Data.DataSet oDataSet = new System.Data.DataSet();
			oDataSet.ReadXml(oXmlTextReader, System.Data.XmlReadMode.Auto);

			oXmlTextReader.Close();
			if(location == RSSLocation.Drive)
				oStreamReader.Close();

			while((intIndex <= oDataSet.Tables.Count - 1) && (intItemTableIndex == -1))
			{
				if(oDataSet.Tables[intIndex].TableName.ToUpper() == feedType.ToString().ToUpper())
					intItemTableIndex = intIndex;

				intIndex++;
			}

			if(intItemTableIndex == -1)
				return(null);
			else
				return(oDataSet.Tables[intItemTableIndex]);
		}

		/// <summary>
		/// Create RSSRoot and write it to stream.
		/// </summary>
		/// <param name="rSSRoot"></param>
		/// <returns></returns>
		public static bool PublishRSS(RSSRoot rSSRoot)
		{
			bool blnResult = true;

			if(rSSRoot == null)
				return(false);

			if(rSSRoot.Channel == null)
				return(false);

			System.Xml.XmlTextWriter oXmlTextWriter = new System.Xml.XmlTextWriter(rSSRoot.OutputStream, System.Text.Encoding.UTF8);

			oXmlTextWriter.WriteStartDocument();

			oXmlTextWriter.WriteStartElement("rss");
			oXmlTextWriter.WriteAttributeString("version", "2.0");

			oXmlTextWriter.WriteStartElement("channel");

			oXmlTextWriter.WriteElementString("link", rSSRoot.Channel.Link);
			oXmlTextWriter.WriteElementString("title", rSSRoot.Channel.Title);
			oXmlTextWriter.WriteElementString("description", rSSRoot.Channel.Description);

			if(rSSRoot.Channel.Docs != "")
				oXmlTextWriter.WriteElementString("docs", rSSRoot.Channel.Docs);

			if(rSSRoot.Channel.PubDate != "")
			{
				System.DateTime sDateTime = System.Convert.ToDateTime(rSSRoot.Channel.PubDate);
				oXmlTextWriter.WriteElementString("pubDate", sDateTime.ToString("ddd, dd MMM yyyy HH:mm:ss G\\MT"));
			}

			if(rSSRoot.Channel.Generator != "")
				oXmlTextWriter.WriteElementString("generator", rSSRoot.Channel.Generator);

			if(rSSRoot.Channel.WebMaster != "")
				oXmlTextWriter.WriteElementString("webMaster", rSSRoot.Channel.WebMaster);

			if(rSSRoot.Channel.Copyright != "")
				oXmlTextWriter.WriteElementString("copyright", rSSRoot.Channel.Copyright);

			if(rSSRoot.Channel.LastBuildDate != "")
			{
				System.DateTime sDateTime = System.Convert.ToDateTime(rSSRoot.Channel.LastBuildDate);
				oXmlTextWriter.WriteElementString("lastBuildDate", sDateTime.ToString("ddd, dd MMM yyyy HH:mm:ss G\\MT"));
			}

			if(rSSRoot.Channel.ManagingEditor != "")
				oXmlTextWriter.WriteElementString("managingEditor", rSSRoot.Channel.ManagingEditor);

			oXmlTextWriter.WriteElementString("language", rSSRoot.Channel.Language.ToString().Replace("_", "-"));

			if(rSSRoot.Image != null)
			{
				oXmlTextWriter.WriteStartElement("image");

				oXmlTextWriter.WriteElementString("url", rSSRoot.Image.URL);
				oXmlTextWriter.WriteElementString("link", rSSRoot.Image.Link);
				oXmlTextWriter.WriteElementString("title", rSSRoot.Image.Title);

				if(rSSRoot.Image.Description != "")
					oXmlTextWriter.WriteElementString("description", rSSRoot.Image.Description);

				if(rSSRoot.Image.Width != 0)
					oXmlTextWriter.WriteElementString("width", rSSRoot.Image.Width.ToString());

				if(rSSRoot.Image.Height != 0)
					oXmlTextWriter.WriteElementString("height", rSSRoot.Image.Height.ToString());

				oXmlTextWriter.WriteEndElement();
			}

			foreach(RSSItem itmCurrent in rSSRoot.Items)
			{
				oXmlTextWriter.WriteStartElement("item");

				if(itmCurrent.Link != "")
					oXmlTextWriter.WriteElementString("link", itmCurrent.Link);

				if(itmCurrent.Title != "")
					oXmlTextWriter.WriteElementString("title", itmCurrent.Title);

				if(itmCurrent.Author != "")
					oXmlTextWriter.WriteElementString("author", itmCurrent.Author);

				if(itmCurrent.PubDate != "")
				{
					System.DateTime sDateTime = System.Convert.ToDateTime(itmCurrent.PubDate);
					oXmlTextWriter.WriteElementString("pubDate", sDateTime.ToString("ddd, dd MMM yyyy HH:mm:ss G\\MT"));
				}

				if(itmCurrent.Comment != "")
					oXmlTextWriter.WriteElementString("comment", itmCurrent.Comment);

				if(itmCurrent.Description != "")
					oXmlTextWriter.WriteElementString("description", itmCurrent.Description);

				oXmlTextWriter.WriteEndElement();
			}

			oXmlTextWriter.WriteEndElement();
			oXmlTextWriter.WriteEndElement();

			oXmlTextWriter.WriteEndDocument();

			oXmlTextWriter.Flush();
			oXmlTextWriter.Close();

			return(blnResult);
		}

		#region Support
		public static string Owner
		{
			get
			{
				return("Dariush Tasdighi - From Tehran, Iran");
			}
		}

		public static string Version
		{
			get
			{
				return("1.0.0");
			}
		}

		public static string Support
		{
			get
			{
				return("Dariush@IranianExperts.com;DariushT@GMail.com;DariushTasdighi@Yahoo.com");
			}
		}

		public static string Homepage
		{
			get
			{
				return("http://www.IranianExperts.com;http://groups.yahoo.com/group/IranianExperts");
			}
		}

		public static string UpdatedDate
		{
			get
			{
				return("14 Feb 2005");
			}
		}
		#endregion
	}
}

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 MIT License


Written By
Web Developer Sematec Ins.
Iran (Islamic Republic of) Iran (Islamic Republic of)
My experiences are:

HTML 5.0, CSS 3.0
JQuery, Angular JS, Bootstrap

MVC 5.0, WEB API, c#

My Site URLs:
http://www.IranianExperts.ir
http://www.IranianExperts.com

My Yahoo Group URL: http://groups.yahoo.com/group/iranianexperts

Mobile: 0098-912-108-7461
Address: Tehran, Tehran, Iran

Comments and Discussions