I was recently working on an EPiServer solution and the client wanted to include an RSS feed on certain pages of their site. I though I would write out here how I consumed the feed. You’ll want to add error handling etc in here, as this is as rough as a tramps beard whilst being enough for you to get the point.
The first thing I had to do was to find out how RSS is structured. After a little Google search I found an article explaining the structure of RSS in sufficient detail for me to continue.
The first thing that I did was create a struck to contain the configuration that I wanted, fairly simple:
/// <summary>
/// RSS feed configuration.
/// </summary>
[Serializable]
struct PropertyRssFeedData
{
private string feedLocation;
private bool linkTitle;
private bool displayRssImage;
private int numberofItemstoDisplay;
public string FeedLocation { get { return this.feedLocation; } set { this.feedLocation = value; } }
public bool LinkTitle { get { return this.linkTitle; } set { this.linkTitle = value; } }
public bool DisplayRssImage { get { return this.displayRssImage; } set { this.displayRssImage = value; } }
public int NumberofItemstoDisplay { get { return this.numberofItemstoDisplay; } set { this.numberofItemstoDisplay = value; } }
public PropertyRssFeedData(string url, int numItems, bool linkTitle, bool rssIcon)
{
this.feedLocation = url;
this.numberofItemstoDisplay = numItems;
this.linkTitle = linkTitle;
this.displayRssImage = rssIcon;
}
}
An instance of this will be serialized/deserialized as versions of the page are edited/created in EPiServer. Essentially I just use it to determine how to render my HTML that I will present to the end-user. Actually reading the RSS is pretty straight forward (this is really being posted as a reminder for me, but might be useful for others). The following code creates a simple HTML block to present our feed.
#region render links and things
internalData = DeserializeData((string)PropertyData.Value);
// Go get the feed
XmlTextReader rssReader = new XmlTextReader(internalData.FeedLocation);
XmlDocument rssDoc = new XmlDocument();
rssDoc.Load(rssReader);
// Look for the <rss> tag
XmlNode nodeRss = rssDoc.SelectSingleNode("//rss");
// ensure we have an RSS node before we continue
if (nodeRss != null)
{
// OK, start creating controls
StringBuilder htmlResult = new StringBuilder();
htmlResult.Append("<div id=\"rssFeed\">" + Environment.NewLine);
// Look for the <channel> tag
XmlNode nodeChannel = nodeRss.SelectSingleNode("//channel");
#region Feed title
htmlResult.Append("\t<div id=\"rssFeedTitle\">");
if (internalData.LinkTitle)
{
htmlResult.AppendFormat(
"<a href=\"{0}\">{1}</a>",
nodeChannel["link"].InnerText,
nodeChannel["title"].InnerText
);
}
else
{
htmlResult.Append(nodeChannel["title"].InnerText);
}
htmlResult.Append("</div>" + Environment.NewLine);
#endregion
#region items
// get the kiddies
if (nodeChannel != null)
{
XmlNodeList itemNodes = nodeChannel.SelectNodes("//item");
if (itemNodes.Count > 0)
{
htmlResult.Append("\t<ul>" + Environment.NewLine);
// we only want to display the maximum set in the
// configuration (otherwise ofc, we coudl use foreach here
for(int counter = 0; counter < internalData.NumberofItemstoDisplay; counter++)
{
htmlResult.Append("\t\t<li>");
// Display the icon or not
if (internalData.DisplayRssImage)
{
// TODO : find a better way of displaying this
htmlResult.AppendFormat(
"<a href=\"{0}\">{1}</a>",
itemNodes[counter]["link"].InnerText,
String.Format(
"<img src=\"{0}\">{1}",
"/rssIcon.gif",
itemNodes[counter]["title"].InnerText
)
);
}
else
{
htmlResult.AppendFormat(
"<a href=\"{0}\">{1}</a>",
itemNodes[counter]["link"].InnerText,
itemNodes[counter]["title"].InnerText
);
}
htmlResult.Append("\t\t</li>");
}
htmlResult.Append("\t</ul>");
}
}
#endregion
Literal output = new Literal();
output.Text = htmlResult.ToString();
this.Controls.Add(output);
}
else
{
// NO RSS TAG
}
#endregion
While I could write about creating the property in EPiServer and wrapping it up all nicely. I’ll be honest, it’s just a PropertyString, so not worth worrying about. Ye sits rough and not very good at the error handling (as I eluded to above), but this is the easy bit. I now have to go and port this back to EPiServer CMS 4.6!