Silverlight: Using Returned XML

Overview

Silverlight provides a rich toolset to work with XML data with the different options suited for different tasks. This page covers the most common ways to work with XML data. For simplicity, all of the examples below use Atom XML data fetched from http://api.flickr.com/services/feeds/photos_public.gne. However, when working with Atom and RSS feeds it is usually easier to use the Silverlight feed classes. See Using RSS and Atom Feeds below.

Using the XmlReader Class

The XmlReader class provides fast read- and forward-only access to data and is well suited for iterating over an XML document. It can read data from a stream or it can be passed a URI to a file that is located within the application's XAP.

Example

The following example shows how to take an existing stream and output the content of the text nodes in the document.

Visual Basic Example

Using the XmlSerializer Class

For well-known data structures, such as business objects, it is often necessary to read external data and import it into internal strongly typed objects. The XmlSerializer class lets you easily deserialize XML into managed objects. The downside of using XmlSerializer is the requirement to reference the external System.Xml.Serialization.dll assembly which will increase your XAP size by approximately 150kB.

Example

The following example shows how to take an existing stream and deserialize the data into strongly-typed objects. The abbreviated data classes are shown after the method.

C# Example
Visual Basic Example

Using XLINQ

Link to XML provides an easier read/write interface than the traditional XML Document Object Model. Integration with LINQ allows you to write queries and modify the contents of the document. The downside of using XLINQ is the requirement to reference the external System.Xml.Linq.dll assembly which will increase your XAP size by approximately 56kB.

Example

The following example shows how to take an existing stream, sort the results based on an element, and output selected data.

C# Example
Visual Basic Example

Using RSS and Atom Feeds

Silverlight provides support for RSS and Atom feeds in the System.ServiceModel.Syndication namespace which makes it very easy to work with these common data structures. The downside of using the feed support is the requirement to reference the external System.ServiceModel.Syndication.dll assembly which will increase your XAP size by approximately 53kB.

Example

The following example shows how to take an existing stream and output the entry titles using feed support.

C# Example
Visual Basic Example

Further Reading

Related information on the web is listed below.