Flux RSS avec System.ServiceModel.Syndication

15. July 2009

Bonjour,

Dans cet article je vais vous exposer un moyen simple de gérer et diffuser des flux RSS 2.0 et Atom 1 pour votre site web ASP.NET par exemple.

Pour ce faire nous allons utiliser les classes du namespace System.ServiceModel.Syndication du Framework 3.5.

.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.

Tout d’abord commençons par créer 2 énumérations que nous utiliserons plus tard lors de la lecture et l’écriture de flux RSS.

public enum FeedFormat
{
Rss,
Atom
}

public struct FeedFormatMimeType
{
public const String Rss20 = "application/rss+xml";
public const String Atom10 = "application/atom+xml";
}

 

Lecture d’un Flux RSS

 

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();
}
}

Ecriture d’un Flux RSS

 

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
}

Utilisation simple avec un 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;
}
}
}

Sortie 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>

Sortie Atom 1

 

<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>

Et voila rien de bien compliqué :)
Shout it

ASP.NET, C# , , ,

Comments