views:

373

answers:

1

I've got some problems parsing the response of a Last.fm API call from a Silverlight 3 application. I pass the response string of the REST service call, which was made via the WebClient class, to the XDocument.Parse() method to get a XDocument.

Parsing the xml string seems to be successful but when I look at the resulting XDocument it shows that the XDocument has only one node containing the whole xml string. So calling any method like Element(nodeName) returns null.

I couldn't image that the result string of the REST service call returns an invalid and not well formed response so probably I do something wrong.


UPDATE

The code I use to parse the XML:

XDocument xml = XDocument(e.result) // e is the response from the WebClient call. 
XElement nameNode = xml.Element("name");

After running this code, the nameNode object is null.

And here is the XML the Last.fm REST service returns without any modification. The \n and \t characters were all part of the response.

<?xml version=\"1.0\" encoding=\"utf-8\"?>\n  <lfm status=\"ok\">
  \n<artist>
    \n  <name>Air</name>\n  <mbid>cb67438a-7f50-4f2b-a6f1-2bb2729fd538</mbid>\n  <url>http://www.last.fm/music/Air&lt;/url&gt;\n  <image size=\"small\">http://userserve-ak.last.fm/serve/34/251119.jpg&lt;/image&gt;\n  <image size=\"medium\">http://userserve-ak.last.fm/serve/64/251119.jpg&lt;/image&gt;\n  <image size=\"large\">http://userserve-ak.last.fm/serve/126/251119.jpg&lt;/image&gt;\n  <image size=\"extralarge\">http://userserve-ak.last.fm/serve/252/251119.jpg&lt;/image&gt;\n  <image size=\"mega\"></image>\n  <streamable>1</streamable>\n  <stats>
      \n    <listeners>1176515</listeners>\n    <playcount>39388973</playcount>\n
    </stats>\n\n  <similar>
      \n\t    <artist>
        \n\t  <name>Zero 7</name>\n\t  <url>http://www.last.fm/music/Zero+7&lt;/url&gt;\n\t  <image size=\"small\">http://userserve-ak.last.fm/serve/34/8612117.jpg&lt;/image&gt;\n\t  <image size=\"medium\">http://userserve-ak.last.fm/serve/64/8612117.jpg&gt;&lt;/image&gt;\n\t  <image size=\"large\">http://userserve-ak.last.fm/serve/126/8612117.jpg&lt;/image&gt;\n\t  <image size=\"extralarge\">http://userserve-ak.last.fm/serve/252/8612117.jpg&lt;/image&gt;\n\t  <image size=\"mega\"></image>\n\t
      </artist>\n    <artist>
        \n\t  <name>Air and Alessandro Baricco</name>\n\t  <url>http://www.last.fm/music/Air+and+Alessandro+Baricco&lt;/url&gt;\n\t  <image size=\"small\">http://userserve-ak.last.fm/serve/34/511739.jpg&lt;/image&gt;\n\t  <image size=\"medium\">http://userserve-ak.last.fm/serve/64/511739.jpg&gt;&lt;/image&gt;\n\t  <image size=\"large\">http://userserve-ak.last.fm/serve/126/511739.jpg&lt;/image&gt;\n\t  <image size=\"extralarge\">http://userserve-ak.last.fm/serve/252/511739.jpg&lt;/image&gt;\n\t  <image size=\"mega\"></image>\n\t
      </artist>\n    <artist>
        \n\t  <name>Röyksopp</name>\n\t  <url>http://www.last.fm/music/R%C3%B6yksopp&lt;/url&gt;\n\t  <image size=\"small\">http://userserve-ak.last.fm/serve/34/22960937.jpg&lt;/image&gt;\n\t  <image size=\"medium\">http://userserve-ak.last.fm/serve/64/22960937.jpg&gt;&lt;/image&gt;\n\t  <image size=\"large\">http://userserve-ak.last.fm/serve/126/22960937.jpg&lt;/image&gt;\n\t  <image size=\"extralarge\">http://userserve-ak.last.fm/serve/252/22960937.jpg&lt;/image&gt;\n\t  <image size=\"mega\">http://userserve-ak.last.fm/serve/_/22960937/Ryksopp+Svein++Torbjorn+Being+Weird.jpg&lt;/image&gt;\n\t
      </artist>\n    <artist>
        \n\t  <name>Télépopmusik</name>\n\t  <url>http://www.last.fm/music/T%C3%A9l%C3%A9popmusik&lt;/url&gt;\n\t  <image size=\"small\">http://userserve-ak.last.fm/serve/34/880851.jpg&lt;/image&gt;\n\t  <image size=\"medium\">http://userserve-ak.last.fm/serve/64/880851.jpg&gt;&lt;/image&gt;\n\t  <image size=\"large\">http://userserve-ak.last.fm/serve/126/880851.jpg&lt;/image&gt;\n\t  <image size=\"extralarge\">http://userserve-ak.last.fm/serve/252/880851.jpg&lt;/image&gt;\n\t  <image size=\"mega\"></image>\n\t
      </artist>\n    <artist>
        \n\t  <name>Thievery Corporation</name>\n\t  <url>http://www.last.fm/music/Thievery+Corporation&lt;/url&gt;\n\t  <image size=\"small\">http://userserve-ak.last.fm/serve/34/172112.jpg&lt;/image&gt;\n\t  <image size=\"medium\">http://userserve-ak.last.fm/serve/64/172112.jpg&gt;&lt;/image&gt;\n\t  <image size=\"large\">http://userserve-ak.last.fm/serve/126/172112.jpg&lt;/image&gt;\n\t  <image size=\"extralarge\">http://userserve-ak.last.fm/serve/252/172112.jpg&lt;/image&gt;\n\t  <image size=\"mega\">http://userserve-ak.last.fm/serve/500/4872357/Thievery+Corporation+thievery_corporation_2.jpg&lt;/image&gt;\n\t
      </artist>\n
    </similar>\n    <tags>
      \n        <tag>
        \n\t  <name>electronic</name>\n\t  <url>http://www.last.fm/tag/electronic&lt;/url&gt;\n\t
      </tag>\n        <tag>
        \n\t  <name>chillout</name>\n\t  <url>http://www.last.fm/tag/chillout&lt;/url&gt;\n\t
      </tag>\n        <tag>
        \n\t  <name>ambient</name>\n\t  <url>http://www.last.fm/tag/ambient&lt;/url&gt;\n\t
      </tag>\n        <tag>
        \n\t  <name>electronica</name>\n\t  <url>http://www.last.fm/tag/electronica&lt;/url&gt;\n\t
      </tag>\n        <tag>
        \n\t  <name>french</name>\n\t  <url>http://www.last.fm/tag/french&lt;/url&gt;\n\t
      </tag>\n
    </tags>\n      <bio>
      \n    <published>Mon, 25 May 2009 19:45:40 +0000</published>\n    <summary><![CDATA[There are at least six artists with this name:\r 1. French electronica duo formed in 1995. (The main producer of music under the name Air)\r 2. Chicago-based jazz trio formed in the early 70s.\r 3. Alias of ambient producer <a href=\"http://www.last.fm/music/Pete+Namlook\" class=\"bbcode_artist\">Pete Namlook</a>.\r 4. Alias of Japanese artist <a href=\"http://www.last.fm/music/Koji+kurumatani\" class=\"bbcode_artist\">Koji kurumatani</a>.\r 5. German krautrock band.\r 6. Lithuanian artist band &quot;Artist in residence&quot;  1) The <a href=\"http://www.last.fm/place/france\"&gt;French&lt;/a&gt; band Air is a duo consisting of <a href=\"http://www.last.fm/music/Nicolas+Godin\" class=\"bbcode_artist\">Nicolas Godin</a> and <a href=\"http://www.last.fm/music/Darkel\"&gt;Jean-Benoit Dunckel</a>.]]></summary>\n    <content><![CDATA[There are at least six artists with this name:\r 1. French electronica duo formed in 1995. (The main producer of music under the name Air)\r 2. Chicago-based jazz trio formed in the early 70s.\r 3. Alias of ambient producer <a href=\"http://www.last.fm/music/Pete+Namlook\" class=\"bbcode_artist\">Pete Namlook</a>.\r 4. Alias of Japanese artist <a href=\"http://www.last.fm/music/Koji+kurumatani\" class=\"bbcode_artist\">Koji kurumatani</a>.\r 5. German krautrock band.\r 6. Lithuanian artist band &quot;Artist in residence&quot;\r \r <strong>1)</strong> The <a href=\"http://www.last.fm/place/france\"&gt;French&lt;/a&gt; band <strong>Air</strong> is a duo consisting of <a href=\"http://www.last.fm/music/Nicolas+Godin\" class=\"bbcode_artist\">Nicolas Godin</a> and <a href=\"http://www.last.fm/music/Darkel\"&gt;Jean-Benoit Dunckel</a>. They went to school in Versailles, Paris (Lycée Jules Ferry) before forming the band in 1995. Their critically acclaimed first album <a title=\"Air - Premiers sympt&ocirc;mes\" href=\"http://www.last.fm/music/Air/Premiers+sympt%C3%B4mes\" class=\"bbcode_album\">Premiers sympt&ocirc;mes</a> was followed by the release of <a title=\"Air - Moon Safari\" href=\"http://www.last.fm/music/Air/Moon+Safari\" class=\"bbcode_album\">Moon Safari</a>, <a title=\"Air - The Virgin Suicides\" href=\"http://www.last.fm/music/Air/The+Virgin+Suicides\" class=\"bbcode_album\">The Virgin Suicides</a> (soundtrack), <a title=\"Air - 10,000hz Legend\" href=\"http://www.last.fm/music/Air/10%2C000hz+Legend\" class=\"bbcode_album\">10,000hz Legend</a>, and <a title=\"Air - Talkie Walkie\" href=\"http://www.last.fm/music/Air/Talkie+Walkie\" class=\"bbcode_album\">Talkie Walkie</a>. In 2007, Air released the album <a title=\"Air - Pocket Symphony\" href=\"http://www.last.fm/music/Air/Pocket+Symphony\" class=\"bbcode_album\">Pocket Symphony</a>.\r \r Although Air's music is often referred to as <a href=\"http://www.last.fm/tag/electronica\" class=\"bbcode_tag\" rel=\"tag\">electronica</a> or <a href=\"http://www.last.fm/tag/trip-hop\" class=\"bbcode_tag\" rel=\"tag\">trip-hop</a> and is often found in such bins in record stores, their form of electronic music actually owes more to the synthesizer sounds of the 19<a href=\"http://www.last.fm/tag/70s\" class=\"bbcode_tag\" rel=\"tag\">70s</a> such as <a href=\"http://www.last.fm/music/+noredirect/Jean+Michel+Jarre\" class=\"bbcode_artist\">Jean Michel Jarre</a> and <a href=\"http://www.last.fm/music/Vangelis\" class=\"bbcode_artist\">Vangelis</a>. Other influences that are more readily apparent than <a href=\"http://www.last.fm/tag/house\" class=\"bbcode_tag\" rel=\"tag\">house</a> and <a href=\"http://www.last.fm/tag/techno\" class=\"bbcode_tag\" rel=\"tag\">techno</a> are <a href=\"http://www.last.fm/tag/psychedelic%20rock\" class=\"bbcode_tag\" rel=\"tag\">psychedelic rock</a> pioneers <a href=\"http://www.last.fm/music/Pink+Floyd\" class=\"bbcode_artist\">Pink Floyd</a> and <a href=\"http://www.last.fm/tag/krautrock\" class=\"bbcode_tag\" rel=\"tag\">krautrock</a>ers <a href=\"http://www.last.fm/music/Tangerine+Dream\" class=\"bbcode_artist\">Tangerine Dream</a> (although there are some echoes of dance music styles in the production). Another influence is French crooner <a href=\"http://www.last.fm/music/Serge+Gainsbourg\" class=\"bbcode_artist\">Serge Gainsbourg</a>. Air's music also has <a href=\"http://www.last.fm/tag/jazz\" class=\"bbcode_tag\" rel=\"tag\">jazz</a> inflections at times, and their ability to improvise is more clearly highlighted in the live arena. Before they founded Air, JB and Nicolas played together in the band <a href=\"http://www.last.fm/music/Orange\" class=\"bbcode_artist\">Orange</a>, with others such as <a href=\"http://www.last.fm/music/Alex+Gopher\" class=\"bbcode_artist\">Alex Gopher</a>, <a href=\"http://www.last.fm/music/Xavier+Jamaux\" class=\"bbcode_artist\">Xavier Jamaux</a> and <a href=\"http://www.last.fm/music/+noredirect/Etienne+De+Crecy\" class=\"bbcode_artist\">Etienne De Crecy</a> names which have recently reappeared in connection with Air's remixes.\r \r Air uses many of their studio instruments (like Moog synthesizers, the Korg MS20, Wurlitzer and Vocoder) live on stage. The band performs the well-known tracks from the albums live as extended or altered versions. Air often works together, both in the studio and live on stage, with artists like <a href=\"http://www.last.fm/music/Beth+Hirsch\" class=\"bbcode_artist\">Beth Hirsch</a> (Moon Safari), <a href=\"http://www.last.fm/music/Fran%C3%A7oise+Hardy\" class=\"bbcode_artist\">Fran&ccedil;oise Hardy</a> (&quot;Jeanne&quot;), <a href=\"http://www.last.fm/music/+noredirect/Jean-Jaques+Perrey\" class=\"bbcode_artist\">Jean-Jaques Perrey</a> (&quot;Cosmic Bird&quot;), <a href=\"http://www.last.fm/music/Gordon+Tracks\" class=\"bbcode_artist\">Gordon Tracks</a> (&quot;Playground Love&quot; and &quot;Easy Going Woman&quot;), <a href=\"http://www.last.fm/music/Beck\" class=\"bbcode_artist\">Beck</a> (10,000Hz Legend) and, on the 2004 tour, with <a href=\"http://www.last.fm/music/Dave+Palmer\" class=\"bbcode_artist\">Dave Palmer</a> and drummer Earl Harvin. On their 2007 album &quot;<a title=\"Air - Pocket Symphony\" href=\"http://www.last.fm/music/Air/Pocket+Symphony\" class=\"bbcode_album\">Pocket Symphony</a>&quot;, more guest writer and vocalist appearances were made by <a href=\"http://www.last.fm/music/Jarvis+Cocker\" class=\"bbcode_artist\">Jarvis Cocker</a> and <a href=\"http://www.last.fm/music/Neil+Hannon\" class=\"bbcode_artist\">Neil Hannon</a> (of <a href=\"http://www.last.fm/music/The+Divine+Comedy\" class=\"bbcode_artist\">The Divine Comedy</a>. For the 2007 tour that accompanied the record, the band consisted of Earl Harvin on drums, multi-instrumentalist Steve Jones (<span class=\"userIcon\"><a href=\"http://www.last.fm/user/steviejjones\" class=\"bbcode_user\">steviejjones</a></span>) and French keyboard player Vincent Taurelle.\r \r Their music has also frequently been used in <a href=\"http://www.last.fm/tag/commercial\" class=\"bbcode_tag\" rel=\"tag\">commercial</a>s. &quot;<a title=\"Air &ndash; Surfing on a Rocket\" href=\"http://www.last.fm/music/Air/_/Surfing+on+a+Rocket\" class=\"bbcode_track\">Surfing on a Rocket</a>&quot; is the background music for the Nissan Armada commercial (2005). &quot;<a title=\"Air &ndash; Playground Love\" href=\"http://www.last.fm/music/Air/_/Playground+Love\" class=\"bbcode_track\">Playground Love</a>&quot; was used in a Levi's jeans commercial (2002). A number of their songs are used as the background music for the BBC 2 show &quot;Top Gear&quot; in their films about exotic sports cars.\r \r They recently contributed to <a href=\"http://www.last.fm/music/Charlotte+Gainsbourg\" class=\"bbcode_artist\">Charlotte Gainsbourg</a>'s second album, &quot;5:55&quot;.\r \r \r <strong>2)</strong> <strong>Air</strong> was a jazz trio with its roots steeped in Chicago's Association for the Advancement of Creative Musicians (AACM), and was formed originally in 1971 or 1972.\r \r The jazz trio Air formed from within the <a href=\"http://www.last.fm/music/Muhal+Richard+Abrams\" class=\"bbcode_artist\">Muhal Richard Abrams</a> Sextet, which was active on the Chicago scene contemporaneously with the birth and growth of the <a href=\"http://allmusic.com/cg/amg.dll?p=amg&amp;amp;token=ADFEAEE47B16D249A97E20D7863A4DCABC7EF329D350EFC01B245F498E830260843520CB40A7C6CCB2E577B479A8B32DAE580AD9CEED469CA1&amp;amp;sql=11:16ge4j474wai\" rel=\"nofollow\">AACM</a> and groups such as the <a href=\"http://www.last.fm/music/Art+Ensemble+of+Chicago\" class=\"bbcode_artist\">Art Ensemble of Chicago</a>. <a href=\"http://www.last.fm/music/Henry+Threadgill\" class=\"bbcode_artist\">Henry Threadgill</a> on reeds, hubcaphone, and percussion; <a href=\"http://www.last.fm/music/Fred+Hopkins\" class=\"bbcode_artist\">Fred Hopkins</a> on bass, and <a href=\"http://www.last.fm/music/Steve+McCall\" class=\"bbcode_artist\">Steve McCall</a> on drums formed the trio when Threadgill was commissioned by Columbia College to score and adapt <a href=\"http://www.last.fm/music/Scott+Joplin\" class=\"bbcode_artist\">Scott Joplin</a>'s work for a performance honoring Joplin's centenary (Joplin's birth has been established now as sometime in 1868) and a rebirth of interest in the noted ragtime composer. All three men were Air signs in the zodiac, and so adopted the name.\r \r They performed first around Chicago, while maintaining residency with Muhal Abrams group, which also featured <a href=\"http://www.last.fm/music/Kalaparusha+Maurice+Mcintyre\" class=\"bbcode_artist\">Kalaparusha Maurice Mcintyre</a> and Wallace McMillan, both reed players. The Air &quot;brand&quot; remained a side project until all three reunited in New York in 1975, and began recording under that name for <a href=\"http://www.last.fm/label/India+Navigation/\" class=\"bbcode_label\">India Navigation</a>, <a href=\"http://www.last.fm/label/nessa/\" class=\"bbcode_label\">nessa</a>, <a href=\"http://www.last.fm/label/Black+Saint/\" class=\"bbcode_label\">Black Saint</a>, and <a href=\"http://www.last.fm/label/Novus/\" class=\"bbcode_label\">Novus</a>. The trio broke up when Steve McCall left in the early 1980s, and when <span title=\"Unknown artist\" class=\"bbcode_unknown\">Pheroan Aklaff</span> joined in 1982, they recorded as <a href=\"http://www.last.fm/music/New+Air\" class=\"bbcode_artist\">New Air</a>, and recorded several albums under that name, including Air Show No. 1 in 1986, which featured <a href=\"http://www.last.fm/music/Cassandra+Wilson\" class=\"bbcode_artist\">Cassandra Wilson</a> on vocals. <a href=\"http://www.last.fm/music/Andrew+Cyrille\" class=\"bbcode_artist\">Andrew Cyrille</a> ultimately replaced akLaff before the band eventually was superceded by the burgeoning careers of its members: Threadgill with his groups Sextett and Very Very Circus, and Hopkins as the go-to bassist for Threadgill, <a href=\"http://www.last.fm/music/David+Murray\" class=\"bbcode_artist\">David Murray</a>, and <a href=\"http://www.last.fm/music/Hamiet+Bluiett\" class=\"bbcode_artist\">Hamiet Bluiett</a>. McCall's health forced his retirement from touring around 1980, and the dynamic drummer and effusive and gracious man who was a cofounder of the AACM with Muhal Abrams and <a href=\"http://www.last.fm/music/Fred+Anderson\" class=\"bbcode_artist\">Fred Anderson</a>, died of a stroke in 1989.\r \r \r <strong>4)</strong> Japanese noise rock, <strong>Air</strong> is the alias of <a href=\"http://www.last.fm/music/Koji+kurumatani\" class=\"bbcode_artist\">Koji kurumatani</a>.\r \r \r ]]></content>\n
    </bio>\n
  </artist>
</lfm>\n
A: 

How did you determine that everything was in one element? Is doc.FirstNode == doc.LastNode? What is doc.NodeType?

If you are only looking at doc.Root.Value, that won't help, as it contains the concatenation of all the descendent text nodes.

Also, you should post at least a little of the XML document causing this problem.


From your example, you're parsing the XML wrong. XDocument.Element gets the first child element with the given name. "name" is not a child of the document.

John Saunders
Thx. So I think I mixed up Element and Descendants.
Flo