// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) 2006 Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Xml;
using Microsoft.Feeds.Interop;
namespace Microsoft.Samples.RssPlatform.ScreenSaver.Rss
{
///
/// Representation of an RSS element in a RSS 2.0 XML document
///
public class RssFeed : UI.IItem
{
private readonly string title;
private readonly string link;
private readonly string description;
private readonly string path;
private readonly DateTime lastWriteTime;
private List items;
public string Title { get { return title; } }
public string Link { get { return link; } }
public string Description { get { return description; } }
public string Path { get { return path; } }
public DateTime LastWriteTime { get { return lastWriteTime; } }
public IList Items { get { return items.AsReadOnly(); } }
///
/// Private constructor to be used with factory pattern.
///
/// Occurs when the XML is not well-formed.
/// An XML block containing the RSSFeed content.
private RssFeed(XmlNode xmlNode)
{
XmlNode channelNode = xmlNode.SelectSingleNode("rss/channel");
items = new List();
title = channelNode.SelectSingleNode("title").InnerText;
link = channelNode.SelectSingleNode("link").InnerText;
description = channelNode.SelectSingleNode("description").InnerText;
XmlNodeList itemNodes = channelNode.SelectNodes("item");
foreach (XmlNode itemNode in itemNodes)
{
RssItem rssItem = new RssItem(itemNode);
// only add items that have enclosures
if (rssItem.Enclosure != null)
items.Add(rssItem);
}
}
private RssFeed(IFeed feed)
{
items = new List();
title = feed.Title;
link = feed.Link;
description = feed.Description;
path = feed.Path;
lastWriteTime = feed.LastWriteTime;
foreach (IFeedItem item in (IFeedsEnum)feed.Items)
{
RssItem rssItem = new RssItem(item);
// only add items that have enclosures
if (rssItem.Enclosure != null)
items.Add(rssItem);
}
}
///
/// Factory that constructs RSSFeed objects from a uri pointing to a valid RSS 2.0 XML file.
///
/// Occurs when the uri cannot be located on the web.
/// The URL to read the RSS feed from.
///
/*
* try
{
Uri url = new Uri("http://localhost/feed.rss");
RssFeed.FromUri(url);
}
catch (UriFormatException ex)
{
MessageBox.Show(ex.Message, "Not a valid Url", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (System.Net.WebException ex)
{
MessageBox.Show(ex.Message, "Failed to get Url", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (System.Xml.XmlException ex)
{
MessageBox.Show(ex.Message, "Not a valid RSS feed.", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
MessageBox.Show("Valid RSS feed.", "Valid RSS feed.", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
*/
///
public static RssFeed FromUri(Uri uri)
{
XmlDocument xmlDoc;
WebClient webClient = new WebClient();
using (Stream rssStream = webClient.OpenRead(uri))
{
TextReader textReader = new StreamReader(rssStream);
XmlTextReader reader = new XmlTextReader(textReader);
xmlDoc = new XmlDocument();
xmlDoc.Load(reader);
}
return new RssFeed(xmlDoc);
}
///
/// Factory to construct the RSSFeed object from the Windows RSS Platform API
///
/// The Common Feed List feed object
/// An initialized RSSFeed object
internal static RssFeed FromApi(IFeed feed)
{
RssFeed rssFeed = null;
// Skip this feed if there are not items.
if (feed != null
&& ((IFeedsEnum)feed.Items).Count > 0)
rssFeed = new RssFeed(feed);
return rssFeed;
}
///
/// Factory that constructs RssFeed objects from the text of an RSS 2.0 XML file.
///
/// A string containing the XML for the RSS feed.
public static RssFeed FromText(string rssText)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(rssText);
return new RssFeed(xmlDoc);
}
}
}