Using Syndication Feed Classes
Silverlight provides classes such as SyndicationFeed and SyndicationItem that can be used to parse and iterate through RSS and ATOM syndication feeds. These specialized classes reside in the System.ServiceModel.Syndication.dll assembly and System.ServiceModel.Syndication namespace. You'll typically use the SyndicationFeed class to start the process of parsing an RSS 2.0 or ATOM 1.0 feed.
If the syndication feed is in a different domain from the Silverlight application, a cross-domain policy file must be defined at the root of the server where the feed is hosted.
The SyndicationFeed class provides a Load method that accepts an XmlReader object instance containing syndication feed data. The XmlReader can stream data received from objects such as WebClient or HttpWebResponse. An example of using the SyndicationFeed class's Load method to parse data retrieved using HttpWebRequest and HttpWebRequest objects follows:
private void StartSyndicationFeedRequest() {
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(
new Uri(this.txtUrl.Text)); AsyncCallback callback = new AsyncCallback(FeedResponseCallback); request.BeginGetResponse(callback, request);
private void FeedResponseCallback (IAsyncResult asyncResult) {
XmlReader reader = null; HttpWebResponse response = null;
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState; response = (HttpWebResponse)request.EndGetResponse(asyncResult); Stream dataStream = response.GetResponseStream(); reader = XmlReader.Create(dataStream); //Load Syndication Feed
SyndicationFeed feed = SyndicationFeed.Load(reader);
Dispatcher.BeginInvoke(() => BindItems(feed));
finally {
reader.Close(); response.Close();
private void BindItems(SyndicationFeed feed) {
this.lbRssItems.ItemsSource = feed.Items;
After feed data has been loaded into memory using the Load method, the SyndicationFeed class's Items property can be called to access the items or entries depending on the feed type. Items is of type IEnumerable<SyndicationItem>, so it can be bound to various Silverlight controls. The SyndicationFeed class also allows new items to be created by calling methods like CreateItem and CreateCategory, and it can save items using methods like SaveAsAtom10 and SaveAsRss20.
The SyndicationItem class provides several properties that can be used to access an item's title, link, author, publication date, text content, category, and so on. It also provides methods that can be used to create feed items such as CreateCategory, CreatePerson, and CreateLink. An example of using the SyndicationItem class to display details about syndication feed items is shown next:
private void ShowFeedItem(SyndicationItem item) {
string content = null;
if (this.gridContent.Visibility == Visibility.Visible) return; this.gridContent.Visibility = Visibility.Visible; HtmlCleaner cleaner = new HtmlCleaner();
this.tbTitle.Text = (string)cleaner.Convert(item.Title.Text, null, null, System.Globalization.CultureInfo.InvariantCulture);
content = (string)cleaner.Convert(item.Summary.Text, null, null, System.Globalization.CultureInfo.InvariantCulture);
if (item.Content != null && item.Content.Type.ToLower() == "html") {
TextSyndicationContent textContent =
item.Content as TextSyndicationContent; content = (string)cleaner.Convert(textContent.Text, null, null, System.Globalization.CultureInfo.InvariantCulture);
this.tbContent.Text = content; this.hlLink.NavigateUri = item.Links[0].Uri; this.tbPubDateRun.Text = item.PublishDate.ToString("d");
Syndication feed item content is accessed differently depending on whether the feed follows the RSS 2.0 or ATOM 1.0 formats. To access RSS 2.0 content, you use the Syndicationltem class's Summary property. The code example feeds the Summary property's Text content through an HTML cleaning method to remove HTML characters so they're not displayed in the Silverlight application. ATOM 1.0 content can be accessed using the Syndicationltem class's Content property, which returns a SyndicationContent object. The code example verifies that the content isn't null and checks the type of the content using the Type property to ensure that it's HTML. It then casts the SyndicationContent object to a TextSyndicationContent object so that the text can be retrieved and cleaned. Figure 9-5 shows the output generated from calling the ShowFeedltem method in the previous code sample.
- Figure 9-5
The syndication feed classes available in Silverlight provide a way to access data without resorting to custom XML parsing or serialization code. By using classes such as SyndicationFeed and SyndicationItem, you can read feed content with a minimal amount of code and effort.
Post a comment