Skip to content

Instantly share code, notes, and snippets.

@mattberther
Created September 21, 2012 17:28
Show Gist options
  • Save mattberther/3762779 to your computer and use it in GitHub Desktop.
Save mattberther/3762779 to your computer and use it in GitHub Desktop.
MattBerther.Syndication
/////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2003 Matt Berther
//
// This material is provided "as is", with absolutely no warranty
// expressed or implied. Any use is at your own risk. Permission to
// use or copy this software for any purpose is hereby granted without
// fee, provided the above notices are retained on all copies.
// Permission to modify the code and to distribute modified code is
// granted, provided the above notices are retained, and a notice that
// the code was modified is included with the above copyright notice.
//
/////////////////////////////////////////////////////////////////////////////
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Reflection;
using System.Runtime.CompilerServices;
[assembly: AssemblyTitle("MattBerther.Syndication")]
[assembly: AssemblyDescription("Syndication Utilities")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("http://www.mattberther.com")]
[assembly: AssemblyProduct("MattBerther.Syndication")]
[assembly: AssemblyCopyright("Copyright (c) 2003")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AssemblyVersion("1.0.0.1")]
namespace MattBerther.Syndication
{
internal class Namespace
{
public const string DublinCore = "http://purl.org/dc/elements/1.1/";
}
/// <summary>
/// A class to deserialize RSS Xml
/// </summary>
/// <example><code>
/// RssFeed theFeed = RssFeed.Create("http://www.mattberther.com/index.xml");
/// </code></example>
[XmlRoot("rss")]
public class RssFeed
{
private RssChannel _channel;
/// <summary>
/// The <see cref="RssChannel"/>
/// </summary>
[XmlElement("channel")]
public RssChannel Channel
{
get { return _channel; }
set { _channel = value; }
}
/// <summary>
/// Loads an <see cref="RssFeed"/> from the specified URL.
/// </summary>
/// <param name="url">URL for the file containing the RSS XML document to load.</param>
/// <returns>The <see cref="RssFeed"/></returns>
public static RssFeed Create(string url)
{
XmlDocument doc = new XmlDocument();
doc.Load(url);
return Create(doc);
}
/// <summary>
/// Loads an <see cref="RssFeed"/> from an <see cref="System.Xml.XmlDocument"/>.
/// </summary>
/// <param name="xml"><see cref="System.Xml.XmlDocument"/> containing the RSS XML to load.</param>
/// <returns>The <see cref="RssFeed"/></returns>
public static RssFeed Create(XmlDocument xml)
{
XmlReader rdr = new XmlNodeReader(xml.DocumentElement);
// TODO: validate against an RSS 2.0 schema as one becomes available
XmlSerializer ser = new XmlSerializer(typeof(RssFeed));
return (RssFeed)ser.Deserialize(rdr);
}
/// <summary>
/// Loads an <see cref="RssFeed"/> from the specified <see cref="System.IO.Stream"/>.
/// </summary>
/// <param name="strm"><see cref="System.IO.Stream"/> containing the RSS XML to load.</param>
/// <returns>The <see cref="RssFeed"/></returns>
public static RssFeed Create(System.IO.Stream strm)
{
// TODO: validate against an RSS 2.0 schema as one becomes available
XmlSerializer ser = new XmlSerializer(typeof(RssFeed));
return (RssFeed)ser.Deserialize(strm);
}
}
public class RssChannel
{
[XmlElement("title")]
public string Title;
[XmlElement("link")]
public string Link;
[XmlElement("description")]
public string Description;
[XmlElement("language", Namespace=Namespace.DublinCore)]
public string Language;
[XmlElement("Author", Namespace=Namespace.DublinCore)]
public string Author;
[XmlElement("rights", Namespace=Namespace.DublinCore)]
public string Copyright;
[XmlElement("date", Namespace=Namespace.DublinCore)]
public DateTime Date;
[XmlElement("item")]
public RssItem[] Items;
}
public class RssItem
{
[XmlElement("title")]
public string Title;
[XmlElement("link")]
public string Link;
[XmlElement("description")]
public string Description;
[XmlElement("subject", Namespace=Namespace.DublinCore)]
public string Subject;
[XmlElement("date", Namespace=Namespace.DublinCore)]
public DateTime Date;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment