Reading a Feed

Once the feed returns in the Response message from the web service call, the ReadFeed_FromWebClient event handler processes the data (shown in Example 10-3). When a web request is made through DownloadStringAsync, the event handler catches the Response message body in an e.Result string parameter. If a stream is required, the string can be converted to a stream; alternatively, instead of using DownloadStringAsync, you can use the OpenReadAsync method to invoke the web service request. OpenReadAsync requests that a stream be created, which will have access to the response message body.

In ReadFeed_FromWebClient, the string is read into a StringReader object instance, which in turn is used to create an instance of an XmlReader object. The XmlReader is required to read the feed data (either RSS or Atom), because it is formatted as XML. The XmlReader instance is then passed to the SyndicationFeed.Load static method where it is parsed into a SyndicationFeed object and a collection of SyndicationItem child objects. The SyndicationFeed object has properties that are mapped to the standard RSS and Atom XML elements, thus making it easy to take the XML from a feed and use it in an object structure instead of reading it as XML.

Example 10-3. Reading the feed response

C# private void ReadFeed_FromWebClient(object sender, DownloadStringCompletedEventArgs e) { string xml = e.Result; if (xml.Length == 0) return; StringReader stringReader = new StringReader(xml); XmlReader reader = ...

Get Data-Driven Services with Silverlight 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.