Home  >  

Parsing XML in Silverlight

Author photo
| | Comments (1)
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. John Papa's Atom feed John_Papa on Twitter

Comments

1 Comments

This LINQ to XML query accepts an XML string that contains the results from a search using Amazon's web services. Very interesting article! many useful informations :)

Leave a comment


Type the characters you see in the picture above.

Tag Cloud

Poll: Sci-Fi Movies

What's Your Favorite Sci-Fi Movie of All Time?

Vote | View Poll Results | Read Related Blog Entry

Latest Features

  •     Welcome back to the series. This time we are goings to build a really exciting component that will be used to simply display information about the user. Well, you might say why to we need such a component, is there... Continue Reading
  •    Welcome back to our exciting Facebook ActionScript series. In this article we will discuss one of most important (and most exciting) features of the FB platform, it's the publishing of news. We all know when we log in to facebook,... Continue Reading
  • This article provides 10 tips and best practices (in no particular order) for maximizing the benefits that Dojo can bring to your next project. For a more thorough introduction to Dojo, see the article Dojo: The JavaScript Toolkit with... Continue Reading
  •     The notifications are one of the most interesting (and important) parts of the facebook area. In order to completely understand the Flash side of it, we need to understand the basics of the facebook notification, what it is and how... Continue Reading

Development Series

Get an overview of the tools and technologies that work together to allow developers to build Rich Internet Applications (RIAs) quickly and easily.

facebook icon Facebook Application Development

Anatomy of an Enterprise Flex RIA

Recommended for You

@InsideRIA on Twitter

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.