<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/parsing-xml-in-silverlight.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.34479-</id>
  <updated>2009-11-16T15:23:46Z</updated>
  <title>Comments for Parsing XML in Silverlight (http://www.insideria.com/2008/11/parsing-xml-in-silverlight.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.34479</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/parsing-xml-in-silverlight.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=34479" title="Parsing XML in Silverlight" />
    <published>2008-12-01T02:53:03Z</published>
    <updated>2009-05-14T16:31:36Z</updated>
    <title>Parsing XML in Silverlight</title>
    <summary>Parsing XML from SIlverlight is a whole lot easier with LINQ to XML.</summary>
    <author>
      <name>John Papa</name>
      <uri>http://johnpapa.net</uri>
    </author>
    
    <category term="Blogs" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[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. 
<br/>
<br/>
<strong>Manager</strong>: <em>We need you to pull in some data from our warehouse to our system. Integrate the 2.</em>

<br/>
<strong>Developer</strong>: <em>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?</em>

<br/>
<strong>Manager</strong>: <em>Thanks, I need it by Friday. Good luck!</em>

<br/>
<strong>Developer</strong>: <em>Wait!!!</em>

<br/>
<br/>
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:

<br/>
<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>

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

</div></div> 

<br/>
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.
<br/>
<br/>
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. 

]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.34479-comment:2054687</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.34479" type="text/html" href="http://www.insideria.com/2008/11/parsing-xml-in-silverlight.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/11/parsing-xml-in-silverlight.html#comment-2054687" />
    <title>Comment from pozycjonowanie stron on 2009-03-07</title>
    <author>
        <name>pozycjonowanie stron</name>
        <uri>http://www.londonrecovery.co.uk</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.londonrecovery.co.uk">
        <![CDATA[<p>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 :)</p>]]>
    </content>
    <published>2009-03-07T15:38:05Z</published>
  </entry>

</feed
