///
/// Creates formatters for RSS 2.0 and Atom 1.0 according to the input content.
///
public static class SyndicationFormatterFactory
{
static XmlReaderSettings settings;
static SyndicationFormatterFactory()
{
// Makes the processing faster for the readers we create.
settings = new XmlReaderSettings();
settings.IgnoreComments = true;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreWhitespace = true;
settings.CheckCharacters = true;
settings.CloseInput = true;
}
///
/// Creates a according to the
/// input format.
///
/// Feed location
/// The input does not contain a valid RSS 2.0 or Atom 1.0 feed.
public static SyndicationFeedFormatter CreateFeedFormatter(string uriString)
{
using (XmlReader reader = XmlReader.Create(uriString, settings))
return CreateFeedFormatter(reader);
}
///
/// Creates a according to the
/// input format.
///
/// Feed location
/// The input does not contain a valid RSS 2.0 or Atom 1.0 feed.
public static SyndicationFeedFormatter CreateFeedFormatter(Uri uri)
{
using (XmlReader reader = XmlReader.Create(uri.ToString(), settings))
return CreateFeedFormatter(reader);
}
///
/// Creates a according to the
/// input format.
///
/// Feed source
/// The input does not contain a valid RSS 2.0 or Atom 1.0 feed.
public static SyndicationFeedFormatter CreateFeedFormatter(XmlReader reader)
{
if (reader.ReadState == ReadState.Initial)
{
reader.MoveToContent();
}
Rss20FeedFormatter rss = new Rss20FeedFormatter();
if (rss.CanRead(reader))
{
return rss;
}
Atom10FeedFormatter atom = new Atom10FeedFormatter();
if (atom.CanRead(reader))
{
return atom;
}
throw new NotSupportedException("Invalid feed root element: " + reader.Name);
}
///
/// Creates a according to the
/// input format.
///
/// Item location
/// The input does not contain a valid RSS 2.0 or Atom 1.0 item.
public static SyndicationItemFormatter CreateItemFormatter(string uriString)
{
using (XmlReader reader = XmlReader.Create(uriString, settings))
return CreateItemFormatter(reader);
}
///
/// Creates a according to the
/// input format.
///
/// Item location
/// The input does not contain a valid RSS 2.0 or Atom 1.0 item.
public static SyndicationItemFormatter CreateItemFormatter(Uri uri)
{
using (XmlReader reader = XmlReader.Create(uri.ToString(), settings))
return CreateItemFormatter(reader);
}
///
/// Creates a according to the
/// input format.
///
/// Item source
/// The input does not contain a valid RSS 2.0 or Atom 1.0 item.
public static SyndicationItemFormatter CreateItemFormatter(XmlReader reader)
{
if (reader.ReadState == ReadState.Initial)
{
reader.MoveToContent();
}
Rss20ItemFormatter rss = new Rss20ItemFormatter();
if (rss.CanRead(reader))
{
return rss;
}
Atom10ItemFormatter atom = new Atom10ItemFormatter();
if (atom.CanRead(reader))
{
return atom;
}
throw new NotSupportedException("Invalid item element: " + reader.Name);
}
}