using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Xml; namespace $safeprojectname$.Rss { /// /// Representation of an RSS element in a RSS 2.0 XML document /// public class RssFeed { private List channels; public IList Channels { get { return channels.AsReadOnly(); } } public RssChannel MainChannel { get { return Channels[0]; } } /// /// Private constructor to be used with factory pattern. /// /// An XML block containing the RSSFeed content. private RssFeed(XmlNode xmlNode) { channels = new List(); // Read the tag XmlNode rssNode = xmlNode.SelectSingleNode("rss"); // For each node in the node // add a channel. XmlNodeList channelNodes = rssNode.ChildNodes; foreach (XmlNode channelNode in channelNodes) { RssChannel newChannel = new RssChannel(channelNode); channels.Add(newChannel); } } /// /// 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. public static RssFeed FromUri(string 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 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); } } }