Monday, September 7, 2009

How the WCF Syndication Object Model Maps to Atom and RSS

How the WCF Syndication Object Model Maps to Atom and RSS
When developing a Windows Communication Foundation (WCF) syndication service, you create feeds and items using the following classes:
A SyndicationFeed can be serialized into any syndication format for which a formatter is defined. WCF ships with two formatters: Atom10FeedFormatter and Rss20FeedFormatter.
The object model around SyndicationFeed and SyndicationItem is aligned more closely with the Atom 1.0 specification than the RSS 2.0 specification. This is because Atom 1.0 is a more substantial specification that defines elements that are ambiguous or omitted from the RSS 2.0 specification. Because of this, many items in the WCF syndication object model have no direct representation in the RSS 2.0 specification. When serializing SyndicationFeed and SyndicationItem objects into RSS 2.0, WCF allows you to serialize Atom-specific data elements as namespace-qualified extension elements that conform to the Atom specification. You can control this with a parameter passed to the Rss20FeedFormatter constructor.
The code samples in this topic use one of two methods defined here to do the actual serialization.
SerializeFeed serializes a syndication feed.
Visual Basic

Public Sub SerializeFeed(ByVal feed As SyndicationFeed)

    Dim atomFormatter As Atom10FeedFormatter = feed.GetAtom10Formatter()
    Dim rssFormatter As Rss20FeedFormatter = feed.GetRss20Formatter()

    Dim atomWriter As XmlWriter = XmlWriter.Create("atom.xml")
    Dim rssWriter As XmlWriter = XmlWriter.Create("rss.xml")
    atomFormatter.WriteTo(atomWriter)
    rssFormatter.WriteTo(rssWriter)
    atomWriter.Close()
    rssWriter.Close()
End Sub
C#

public void SerializeFeed(SyndicationFeed feed)
{
    Atom10FeedFormatter atomFormatter = feed.GetAtom10Formatter();
    Rss20FeedFormatter rssFormatter = feed.GetRss20Formatter();

    XmlWriter atomWriter = XmlWriter.Create("atom.xml");
    XmlWriter rssWriter = XmlWriter.Create("rss.xml");
    atomFormatter.WriteTo(atomWriter);
    rssFormatter.WriteTo(rssWriter);
    atomWriter.Close();
    rssWriter.Close();
}
SerializeItem serializes a syndication item.
Visual Basic

Public Sub SerializeItem(ByVal item As SyndicationItem)
    Dim atomFormatter As Atom10ItemFormatter = item.GetAtom10Formatter()
    Dim rssFormatter As Rss20ItemFormatter = item.GetRss20Formatter()

    Dim atomWriter As XmlWriter = XmlWriter.Create("atom.xml")
    Dim rssWriter As XmlWriter = XmlWriter.Create("rss.xml")
    atomFormatter.WriteTo(atomWriter)
    rssFormatter.WriteTo(rssWriter)
    atomWriter.Close()
    rssWriter.Close()
End Sub
C#

public void SerializeItem(SyndicationItem item)
{
    Atom10ItemFormatter atomFormatter = item.GetAtom10Formatter();
    Rss20ItemFormatter rssFormatter = item.GetRss20Formatter();

    XmlWriter atomWriter = XmlWriter.Create("atom.xml");
    XmlWriter rssWriter = XmlWriter.Create("rss.xml");
    atomFormatter.WriteTo(atomWriter);
    rssFormatter.WriteTo(rssWriter);
    atomWriter.Close();
    rssWriter.Close();
}

SyndicationFeed

The following code example shows how to serialize the SyndicationFeed class to Atom 1.0 and RSS 2.0.



See full details: http://msdn.microsoft.com/en-us/library/bb412166.aspx

No comments: