using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Xml;
using System.ComponentModel;
/// Thanks to XNA ScreenSaver Project scaffolding code writers for borrowed and modified code.
/// Modification and distribution rights assumed. Please contact if assumption in error.
namespace DarkWynter.Engine.Networking
{
///
/// 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.
public 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 (System.IO.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);
}
public static void FromUriInBackground(string uriAddress, string localAddress, AsyncCompletedEventHandler DownloadFileCallback)
{
XmlDocument xmlDoc = new XmlDocument();
WebClient client = new WebClient();
// Specify that the DownloadFileCallback method gets called
// when the download completes.
client.DownloadFileCompleted += DownloadFileCallback;
// Specify a progress notification handler.
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
client.DownloadFileAsync(new Uri(uriAddress), localAddress);
}
private static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
//progress.Value = e.ProgressPercentage;
// Displays the operation identifier, and the transfer progress.
//Console.WriteLine("{0} downloaded {1} of {2} bytes. {3} % complete...",
// (string)e.UserState,
// e.BytesReceived,
// e.TotalBytesToReceive,
// e.ProgressPercentage);
}
///
/// 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);
}
}
}