Try it: Create an RSS news reader
Web feeds such as news feeds or podcasts often use XML-formatted files for their content. You can use programs called readers or aggregators to subscribe to web feeds and read them. Often, a reader will stay open on your computer and will update itself with new content when the web feed is updated. The most common delivery mechanism of web feeds is named Really Simple Syndication (RSS).
The following procedures show you how to create a simple RSS reader by using Microsoft Expression Blend and an RSS feed. The RSS reader could be used to read any XML data source, even a local file or an XML file on your own website.
The following procedures assume that you have a live connection to the Internet.
Microsoft Silverlight does not support XML data sources. However, you can find information about working with XML data under Parsing XML Data in Silverlight on MSDN.
In the Data panel, click Add Live Data Source , and then click Define New XML Data Source.
The Define New XML Data Source dialog box appears.
Next to Data source name, type "rssDS".
Next to XML file path or URL text box, type "http://rss.msnbc.msn.com/id/3032127/device/rss/rss.xml".
You can use any URL or local path of an XML file in the URL for XML data field. If you want to use a different RSS feed, you can locate the URL by opening a provider's website (for example, http://www.msnbc.com) in a web browser and searching for a link to their RSS feeds. Typically, the website will list available feeds, and also buttons that allow you to subscribe to the feed. Look for a link to the XML file for the feed that you want.
Under Define in, do one of the following:
To make the data available to all documents in your application, select Project.
To make the data available only to the current document, select This document.
A data source named rssDS is added in the Data panel. Expand the nodes and examine the different fields of your data source. The data is not displayed, but the names of fields that contain the data and their structure are.
You can now bind controls in your application to the data.
There are multiple ways to bind controls to items in a data source. The following procedure shows you how to drag data source fields from the Data panel onto the artboard to create two new controls. Alternatively, you could drag data source fields onto existing controls to bind the data to properties on those controls.
In the Tools panel, click Assets .
In the Assets panel, select the All in the Controls category.
Click Controls, and then click the Image control .
Using your mouse, draw an image control on the artboard, about 100 pixels wide by 100 pixels high, at the upper-left corner.
In the Data panel, expand the nodes rss, channel, and image. Drag the url : (String) node onto the new image object.
The image object displays the image from the MSNBC website. The Data panel displays a yellow bounding box around the data source to indicate that part of the data has been bound to a control.
In the Data panel, expand the nodes rssDS, rss, and channel. Drag the title : (String) node onto the artboard to the right of the image object.
A TextBlock control is created to display the title of the news feed. Use the Selection tool to move and scale the text block object, and use the properties in the Text and Brushes categories of the Properties panel to change way the text looks.
A data context, when assigned to a parent object, allows all the child objects to use the same snapshot of data. This is useful when you want to create a master-details design, in which, if you click an item in a list (the master pane), the details about that item appear in another object (the details pane).
Create a Grid layout container that covers the area underneath the image and title. The resulting grid object will be your parent object where the data context will be set automatically by the next step.
Select the new grid object in the Objects and Timeline panel to activate it so that you can add child objects.
In the Data panel, make sure List Mode is selected. Items dragged from the Data panel in list mode will create a master control.
Expand the nodes rss, channel, and item (Array). Drag the title : (String) node into the upper-left corner of the grid object.
A ListBox control is created and populated with the titles of all the news stories in the item (Array) collection. If you select the grid object and look in the Properties panel, you'll see a yellow bounding box around the DataContext property to show that it is bound to data. If you select the list box object, you'll see the ItemsSource property bound to data.
The ItemsSource property can be set to any collection of items. Only one of the ItemsSource or Items properties can be used at a time. The ItemsSource property is typically used to bind to a collection of generated items. The Items property can be used to manually add individual items by using the Edit items in this collection button.
In the Data panel, click Details Mode .
Expand the nodes rss, channel, and item (Array). Drag the description : (String) node into the right side of the grid object.
A grid object is created with a text block object inside that displays the description of the selected news story. If you select the text block object and look in the Properties panel, you'll see a yellow bounding box around the Text property to show that it is bound to data. To see the specifics of the data binding, click Advanced property options next to the Text property, and then click Data binding.
Press F5 to run your application, and then click any item in the list box to change the selection to read different news descriptions.
Some items might include HTML text. You could create a value converter that would remove the HTML elements from the Description string, and then you could apply that value converter in the Create Data Binding dialog box. For information about creating a value converter, see Try it: Convert data from one type to another.