Simple RSS Control

This is a simple RSS control.

(To use this sample, one needs the code below plus a Linq-To-Sql data model with the given table in it.)

This is the User Control code-behind…

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;

/// <summary>
/// This control displays Rss content.
/// </summary>
/// <remarks>
/// The code in this control was refactored from the orginal found on 20091128 here...
/// http://code.msdn.microsoft.com/CreateRSSFeedVBNet/Release/ProjectReleases.aspx?ReleaseId=969
/// </remarks>
public partial class EasyControls_RssCreatorControl01 : System.Web.UI.UserControl
{
	#region HelperMethods

	public void DoPageLoad()
	{
		this.Response.Clear();
		this.Response.ContentType = "text/xml";
		var myDataContext = new Aggregator01DbDataContext();

		//Add a where clause here if desired.
		var myRssNewsRecords = from r in myDataContext.RssNewsRecord
							   select r;

		var myRssItemList = new List<System.Xml.Linq.XElement>();
		XElement myRssItemTemp = null;

		foreach (RssNewsRecord myRssNewsRecordTemp in myRssNewsRecords)
		{
			myRssItemTemp = new XElement(
				"item",
				new System.Xml.Linq.XElement("title", myRssNewsRecordTemp.Title),
				new System.Xml.Linq.XElement("link", myRssNewsRecordTemp.Link),
				new System.Xml.Linq.XElement("description", myRssNewsRecordTemp.Description),
				new System.Xml.Linq.XElement("pubDate", DateTime.Now.ToString("r"))
				);

			myRssItemList.Add(myRssItemTemp);
		}

		XDocument myRssResult = new XDocument();
		myRssResult.Declaration = new XDeclaration("1.0", "utf-8", "yes");

		XElement myNode1 = new XElement("rss", "");
		XAttribute myAttribute = new XAttribute("version", "2.0");
		myNode1.Add(myAttribute);

		XElement myNode2 = new XElement(
			"channel",
			new System.Xml.Linq.XElement("title", ConfigurationManager.AppSettings["rss_Title"]),
			new System.Xml.Linq.XElement("link", ConfigurationManager.AppSettings["rss_Link"]),
			new System.Xml.Linq.XElement("description", ConfigurationManager.AppSettings["rss_Description"]),
			new System.Xml.Linq.XElement("pubDate", DateTime.Now.ToString("r")),
			myRssItemList
			);

		myNode1.AddFirst(myNode2);
		myRssResult.AddFirst(myNode1);
		this.Response.Write(myRssResult.ToString());
		this.Response.End();
	}

	#endregion //HelperMethods

	#region HandlerMethods

	protected void Page_Load(object sender, EventArgs e)
	{
		this.DoPageLoad();
	}

	#endregion //HandlerMethods
}

This is the User Control code-infront…

<%@ control language="C#" autoeventwireup="true" codefile="RssCreatorControl01.ascx.cs" inherits="EasyControls_RssCreatorControl01" %>

This is the SQL schema…

USE [YOUR_DATABASE_NAME_GOES_HERE]
GO
/****** Object:  Table [dbo].[RssNewsRecord]    Script Date: 11/30/2009 14:32:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RssNewsRecord]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[RssNewsRecord](
[Description] [varchar](1024) NOT NULL CONSTRAINT [DF_RssNewsRecord_Description]  DEFAULT (''),
[Link] [varchar](128) NOT NULL CONSTRAINT [DF_RssNewsRecord_Link]  DEFAULT (''),
[LiveDate] [datetime] NOT NULL CONSTRAINT [DF_RssNewsRecord_LiveDate]  DEFAULT (getdate()),
[ModifiedBy] [nvarchar](128) NOT NULL CONSTRAINT [DF_RssNewsRecord_ModifiedBy]  DEFAULT (''),
[ModifiedDateTime] [datetime] NOT NULL CONSTRAINT [DF_RssNewsRecord_ModifiedDateTime]  DEFAULT (getdate()),
[ModifiedTimestamp] [timestamp] NOT NULL,
[PkId] [uniqueidentifier] NOT NULL CONSTRAINT [DF_RssNewsRecord_Id]  DEFAULT (newid()),
[Title] [varchar](128) NOT NULL CONSTRAINT [DF_RssNewsRecord_Title]  DEFAULT (''),
CONSTRAINT [Pk_RssNewsRecord_PkId] PRIMARY KEY CLUSTERED
(
[PkId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
CONSTRAINT [Uk_RssNewsRecord_Title] UNIQUE NONCLUSTERED
(
[Title] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING OFF

This is the consuming page code-behind…

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class RssCreatorControl01Test01 : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{

	}
}

This is the consuming page code-infront…

<%@ page language="C#" autoeventwireup="true" codefile="RssCreatorControl01Test01.aspx.cs" inherits="RssCreatorControl01Test01" %>

<%@ register src="EasyControls/RssCreatorControl01.ascx" tagname="RssCreatorControl01" tagprefix="uc1" %>
<uc1:rsscreatorcontrol01 id="RssCreatorControl011" runat="server" />
Advertisements

Author: mkamoski1

n/a