15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

eading syndication feeds sample ❘ 1389<br />

figure 47-2<br />

To use the Syndication API, the assembly System.ServiceModel is referenced with the application. The<br />

OnGetFeed() event h<strong>and</strong>ler method is set to the Click event of the button showing the Get Feed text.<br />

The code needed to read the application is really simple. First, the XML content from the RSS feed is read<br />

into the XmlReader class from the System.Xml namespace. Rss20FeedFormatter accepts an XmlReader<br />

with the ReadFrom() method. For data binding, the Feed property that returns a SyndicationFeed is<br />

assigned to the DataContext of the Window, <strong>and</strong> the Feed.Items property that returns<br />

IEnumerable is assigned to the DataContext of a DockPanel container:<br />

private void OnGetFeed(object sender, RoutedEventArgs e)<br />

{<br />

try<br />

{<br />

using (var reader = XmlReader.Create(textUrl.Text))<br />

{<br />

var formatter = new Rss20FeedFormatter();<br />

formatter.ReadFrom(reader);<br />

this.DataContext = formatter.Feed;<br />

this.feedContent.DataContext = formatter.Feed.Items;<br />

}<br />

}<br />

catch (WebException ex)<br />

{<br />

MessageBox.Show(ex.Message, “Syndication Reader“);<br />

}<br />

}<br />

code snippet SyndicationReader/SyndicationReader.xaml.cs<br />

The XAML code that defines the user interface is shown next. The Title property of the Window class is<br />

bound to the Title.Text property of the SyndicationFeed to display the title of the feed.<br />

In the XAML code, a DockPanel named heading, which contains a Label bound to Title.Text <strong>and</strong> a<br />

Label bound to Description.Text, is defined. Because these labels are contained within the DockPanel<br />

named feedContent, <strong>and</strong> feedContent is bound to the Feed.Items property, these labels give title <strong>and</strong><br />

description information about the current selected item.<br />

A list of items is displayed in a ListBox that uses an ItemTemplate to bind a label to the Title.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!