using System; using System.Data; using System.Collections; namespace Rss { /// /// Summary description for RssChannelBuilder. /// public class RssChannelBuilder : RssChannel { private DataTable dataTable; private string itemTitleField; private string itemDescriptionField; private string itemPubDateField; private string itemLinkField; private string itemLinkFormat; /// /// This object will assist in the building of the channel by /// obtaining the channel's items from the set DataSource. /// public RssChannelBuilder() { } /// /// The DataTable which holds the channel's items. /// public DataTable DataSource { set { this.dataTable = value ; } } /// /// The column name for the item's title. /// public string ItemTitleField { set { this.itemTitleField = value ; } } /// /// The column name for the item's description. /// public string ItemDescriptionField { set { this.itemDescriptionField = value ; } } /// /// The column name for the item's publish date. /// public string ItemPubDateField { set { this.itemPubDateField = value ; } } /// /// The column name that contains the unique ID of the item. /// public string ItemLinkField { set { this.itemLinkField = value ; } } /// /// The formatted string (ala RSSMaster style) of the item's link. /// Example: "http://www.centralchristianev.com/weekend/CurrentSeries.aspx?id={0}"; /// The {0} is replaced with the value found in the ItemLinkField. /// public string ItemLinkFormat { set { this.itemLinkFormat = value ; } } /// /// Call this method to bind the DataSource to the RssChannel. /// public void DataBind() { foreach ( DataRow row in dataTable.Rows ) { RssItem item = new RssItem(); if ( row[ this.itemTitleField ] != null ) item.Title = LoadStringData( row[ this.itemTitleField ] ); if ( row[ this.itemLinkField ] != null ) item.Link = new Uri( itemLinkFormat.Replace("{0}", LoadIntData( row[ this.itemLinkField ] ).ToString() ) ); if ( row[ this.itemDescriptionField ] != null ) item.Description = LoadStringData( row[ this.itemDescriptionField ] ); if ( row[ this.itemPubDateField ] != null ) item.PubDate = LoadDateTimeData( row[ this.itemPubDateField ] ); Items.Add( item ); } } /// /// used to check for a DbNull value and return the default Null /// /// date to be checked for DbNull /// The original value or the default Null value if DbNull private int LoadIntData( object dataToLoad ) { if ( dataToLoad == DBNull.Value ) return -1; else return (int)dataToLoad; } /// /// used to check for a DbNull value and return the default Null /// /// date to be checked for DbNull /// The original value or the default Null value if DbNull private string LoadStringData( object dataToLoad ) { if ( dataToLoad == DBNull.Value ) return ""; else return (string)dataToLoad; } /// /// used to check for a DbNull value and return the default Null /// /// date to be checked for DbNull /// The original value or the default Null value if DbNull private DateTime LoadDateTimeData(object dataToLoad) { if (dataToLoad == DBNull.Value) return DateTime.MinValue; else return (DateTime)dataToLoad; } } }