Hi,
In this article I’ll talk about another simple way to Read and Write RSS Feeds, but here with the new .NET 3.5 Syndication feature.
MSDN :
.NET Framework Class Library
System.ServiceModel.Syndication Namespace
The System.ServiceModel.Syndication namespace contains all of the classes that make up the Syndication Object Model.
In order to make usage of this set of classes, I created a “little” wrapper for Read / Write Syndication Feeds.
Let’s start with FeedFormat and FormatContentMimeType enumerations (we are going to use these later)
public enum FeedFormat
{
Rss,
Atom
}
public struct FeedFormatMimeType
{
public const String Rss20 = "application/rss+xml";
public const String Atom10 = "application/atom+xml";
}
Read Syndication Feed : FeedReader
public static class FeedReader
{
public static SyndicationFeed LoadFrom(String url)
{
if (String.IsNullOrEmpty(url))
throw new ArgumentNullException();
SyndicationFeed feed = null;
using (XmlTextReader r = new XmlTextReader(url))
{
feed = SyndicationFeed.Load(r);
}
return feed;
}
public static String ReadFrom(String url)
{
if (String.IsNullOrEmpty(url))
throw new ArgumentNullException();
SyndicationFeed feed = LoadFrom(url);
if (feed == null)
return String.Empty;
FeedWriter writer = new FeedWriter(FeedFormat.Rss, feed);
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
using (XmlTextWriter xtw = new XmlTextWriter(sw))
{
writer.WriteTo(xtw);
}
}
return sb.ToString();
}
}
Write Syndication Feed : FeedWriter
public class FeedWriter
{
#region Private Properties
private List<SyndicationItem> Items { get; set; }
#endregion
#region Public Properties
public SyndicationFeed Feed { get; set; }
public FeedFormat FeedFormat { get; set; }
public String ContentType { get; private set; }
#endregion
#region Constructors
public FeedWriter(FeedFormat format)
: this(format, new SyndicationFeed())
{
}
public FeedWriter(FeedFormat format, SyndicationFeed feed)
{
FeedFormat = format;
switch (FeedFormat)
{
case FeedFormat.Atom:
ContentType = FeedFormatMimeType.Atom10;
break;
case FeedFormat.Rss:
ContentType = FeedFormatMimeType.Rss20;
break;
default:
ContentType = FeedFormatMimeType.Rss20;
break;
}
Items = new List<SyndicationItem>();
Feed = feed;
}
#endregion
#region Public Methods
public void AddItem(SyndicationItem item)
{
if (!Items.Contains(item))
Items.Add(item);
}
public void WriteTo(Stream stream)
{
Feed.Items = Items;
switch (FeedFormat)
{
case FeedFormat.Rss:
WriteToRss20(stream);
break;
case FeedFormat.Atom:
WriteToAtom10(stream);
break;
default:
WriteToRss20(stream);
break;
}
}
public void WriteTo(XmlWriter xw)
{
Feed.Items = Items;
switch (FeedFormat)
{
case FeedFormat.Rss:
WriteToRss20(xw);
break;
case FeedFormat.Atom:
WriteToAtom10(xw);
break;
default:
WriteToRss20(xw);
break;
}
}
#endregion
#region Private Methods
private void WriteToAtom10(Stream stream)
{
using (XmlTextWriter xtw = new XmlTextWriter(stream, Encoding.UTF8))
{
Atom10FeedFormatter f = new Atom10FeedFormatter(Feed);
f.WriteTo(xtw);
}
}
private void WriteToRss20(Stream stream)
{
using (XmlTextWriter xtw = new XmlTextWriter(stream, Encoding.UTF8))
{
Rss20FeedFormatter f = new Rss20FeedFormatter(Feed, false);
f.WriteTo(xtw);
}
}
private void WriteToAtom10(XmlWriter xw)
{
Atom10FeedFormatter f = new Atom10FeedFormatter(Feed);
f.WriteTo(xw);
}
private void WriteToRss20(XmlWriter xw)
{
Rss20FeedFormatter f = new Rss20FeedFormatter(Feed, false);
f.WriteTo(xw);
}
#endregion
}
Sample Usage with Generic HttpHandler
public class Handler2 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpRequest Request = HttpContext.Current.Request;
HttpResponse Response = HttpContext.Current.Response;
FeedFormat Format = FeedFormat.Rss;
if (!String.IsNullOrEmpty(Request.QueryString["format"]))
{
String s = Request.QueryString["format"];
Format = (FeedFormat)Enum.Parse(typeof(FeedFormat), s, true);
}
FeedWriter writer = new FeedWriter(Format);
writer.Feed.Title = new TextSyndicationContent(".NET Rox Feed !");
for (int i = 0; i < 10; i++)
{
SyndicationItem item = new SyndicationItem("My Item" + i.ToString(), "Contents ...",
new Uri("http://blog.sb2.fr"));
writer.AddItem(item);
}
Response.Clear();
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = writer.ContentType;
writer.WriteTo(Response.OutputStream);
}
public bool IsReusable
{
get
{
return false;
}
}
}
Output :
RSS 2.0
<rss version="2.0">
<channel>
<title>.NET Rox Feed !</title>
<description />
<item>
<link>http://blog.sb2.fr/</link>
<title>My Item0</title>
<description>Contents ...</description>
</item>
<item>
<link>http://blog.sb2.fr/</link>
<title>My Item1</title>
<description>Contents ...</description>
</item>
</channel>
</rss>
Atom 1.0
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text">.NET Rox Feed !</title>
<id>uuid:c891669a-f899-4fb2-94a2-4f712f0c570c;id=1</id>
<updated>2008-12-19T09:48:57Z</updated>
<entry>
<id>uuid:c891669a-f899-4fb2-94a2-4f712f0c570c;id=2</id>
<title type="text">My Item0</title>
<updated>2008-12-19T09:48:57Z</updated>
<link rel="alternate" href="http://blog.sb2.fr/" />
<content type="text">Contents ...</content>
</entry>
<entry>
<id>uuid:c891669a-f899-4fb2-94a2-4f712f0c570c;id=3</id>
<title type="text">My Item1</title>
<updated>2008-12-19T09:48:57Z</updated>
<link rel="alternate" href="http://blog.sb2.fr/" />
<content type="text">Contents ...</content>
</entry>
</feed>
Hope this help’s!
Views(2784)

