Home  >  

Parsing XML in Silverlight

Author photo
| | Comments (0)
AddThis Social Bookmark Button
Long have developers loathed iterating through text data, parsing end of line characters and field separators. Fixed length files and delimited files were the bane of a developer's existence.

Manager: We need you to pull in some data from our warehouse to our system. Integrate the 2.
Developer: Wait ... this data is delimited text files. Is there a schema? How do I know what fields are what? Is the format always the same?
Manager: Thanks, I need it by Friday. Good luck!
Developer: Wait!!!

Maybe I exaggerate a little but you get the idea. Its just not fun to parse data. Even when you know the format, as with XML files, tools such as an XmlReader which can iterate through the data leave a lot to be desired. Searching XML nodes for values, especially since many nodes may not even be required, can be a real pain. Enter LINQ to XML. LINQ to XML makes parsing XML much easier than iterating through rows. So instead of looping through rows of data from a service, such as Amazon, to get the results of a Amazon product search, a query can be written using LINQ to XML to pull out what you need. The format is pretty simple as it looks a lot like SQL. For example:

var bookQuery = from book in bookXml.Descendants(ns + "Item")
    let attributes = book.Element(ns + "ItemAttributes")
    let imageSource =
        (book.Elements(ns + "SmallImage").Any()
             ? book.Element(ns + "SmallImage").Element(ns + "URL").Value
             : string.Empty)
    let year = Int32.Parse((attributes.Element(ns + "PublicationDate").Value).Substring(0, 4))
    where attributes.Elements(ns + "ISBN").Any()
    orderby attributes.Element(ns + "Title").Value ascending
    select new Book
    {
         AmazonASIN = ((string)book.Element(ns + "ASIN")).Trim(),
         ISBN = attributes.Element(ns + "ISBN").Value,
         Title = attributes.Element(ns + "Title").Value,
         Publisher = attributes.Element(ns + "Publisher").Value,
         Year = year,
         HrefLink = new Uri(book.Element(ns + "DetailPageURL").Value),
         Authors = (
                       from author in book.Descendants(ns + "Author")
                       select author.Value
                   ).ToList(),
         ImageSource = imageSource
     };

This LINQ to XML query accepts an XML string that contains the results from a search using Amazon's web services. The format is pretty straightforward, even though I used some more involved constructs in the query. This structure beats iterating through results. Its not strongly typed as it takes the name of the XML nodes and attributes, but the query structure basically asks it to pull out all of the products that have an ISBN gather their information, including the link to its image, and then pull them into a new book class instance. At that point the list of book classes can be bound to the UI and displayed in a snap.

When Silverlight 2 applications communicate with web services, many of them return data as XML or JSON (more on this in another post). While XmlReader can be used to iterate through the data and parse out what is needed, LINQ to XML can really make this much simpler to write and maintain.

Read more from John Papa.

Comments

Leave a comment


Type the characters you see in the picture above.

Tag Cloud

Poll: Resolutions for 2009

What's your top resolution of 2009?

Vote | View Poll Results | Read Related Blog Entry

Latest Features

New Recommended eBooks

Get free updates and all-access to O'Reilly eBooks on your iPhone, Kindle, Sony Reader, or other mobile device.

  • Essential ActionScript 3.0, 1e book JavaScript: The Definitive Guide, 5e  book
  • Programming Collective Intelligence book High Performance Web Sites, 1e book
  • ActionScript 3.0 Cookbook book iPhone Open Application Development book
  • Learning Flex 3, 1e  book JavaScript: The Good Parts book
  • ActionScript 3.0 Design Patterns, 1e  book Beautiful Code, 1e  book
  • RESTful Web Services, 1e  book The AIR 1.5 Cookbook  book
  • Visualizing Data, 1e  book Learning ActionScript 3.0, 1e  book
  • Ajax: The Definitive Guide, 1e  bookCSS: Flex 3 Cookbook book
  • JavaScript & DHTML Cookbook, 2e  book Designing Web Navigation book

Learn more about eBook bundles

Recommended for You

Books Excerpts

ActionScript 3.0 Cookbook cover Read excerpts of RIA books provided by InsideRIA and O'Reilly Digital Media.

Find more book excerpts

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.