使用LINQ to XML从Silverlight 3中的Last.fm REST服务解析XML响应时出现问题

时间:2009-07-05 15:39:07

标签: c# xml silverlight rest silverlight-3.0

我在从Silverlight 3应用程序解析Last.fm API调用的响应时遇到了一些问题。我将REST服务调用的响应字符串(通过WebClient类传递)传递给XDocument.Parse()方法以获取XDocument。

解析xml字符串似乎是成功的,但是当我查看生成的XDocument时,它显示XDocument只有一个节点包含整个xml字符串。因此调用Element(nodeName)之类的任何方法都会返回null。

我无法想象REST服务调用的结果字符串返回无效且格式不正确的响应,所以我可能做错了。


更新

我用来解析XML的代码:

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

运行此代码后,nameNode对象为null。

这是Last.fm REST服务返回的XML,没有任何修改。 \ n和\ t字符都是响应的一部分。

<?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</url>\n  <image size=\"small\">http://userserve-ak.last.fm/serve/34/251119.jpg</image>\n  <image size=\"medium\">http://userserve-ak.last.fm/serve/64/251119.jpg</image>\n  <image size=\"large\">http://userserve-ak.last.fm/serve/126/251119.jpg</image>\n  <image size=\"extralarge\">http://userserve-ak.last.fm/serve/252/251119.jpg</image>\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</url>\n\t  <image size=\"small\">http://userserve-ak.last.fm/serve/34/8612117.jpg</image>\n\t  <image size=\"medium\">http://userserve-ak.last.fm/serve/64/8612117.jpg></image>\n\t  <image size=\"large\">http://userserve-ak.last.fm/serve/126/8612117.jpg</image>\n\t  <image size=\"extralarge\">http://userserve-ak.last.fm/serve/252/8612117.jpg</image>\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</url>\n\t  <image size=\"small\">http://userserve-ak.last.fm/serve/34/511739.jpg</image>\n\t  <image size=\"medium\">http://userserve-ak.last.fm/serve/64/511739.jpg></image>\n\t  <image size=\"large\">http://userserve-ak.last.fm/serve/126/511739.jpg</image>\n\t  <image size=\"extralarge\">http://userserve-ak.last.fm/serve/252/511739.jpg</image>\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</url>\n\t  <image size=\"small\">http://userserve-ak.last.fm/serve/34/22960937.jpg</image>\n\t  <image size=\"medium\">http://userserve-ak.last.fm/serve/64/22960937.jpg></image>\n\t  <image size=\"large\">http://userserve-ak.last.fm/serve/126/22960937.jpg</image>\n\t  <image size=\"extralarge\">http://userserve-ak.last.fm/serve/252/22960937.jpg</image>\n\t  <image size=\"mega\">http://userserve-ak.last.fm/serve/_/22960937/Ryksopp+Svein++Torbjorn+Being+Weird.jpg</image>\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</url>\n\t  <image size=\"small\">http://userserve-ak.last.fm/serve/34/880851.jpg</image>\n\t  <image size=\"medium\">http://userserve-ak.last.fm/serve/64/880851.jpg></image>\n\t  <image size=\"large\">http://userserve-ak.last.fm/serve/126/880851.jpg</image>\n\t  <image size=\"extralarge\">http://userserve-ak.last.fm/serve/252/880851.jpg</image>\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</url>\n\t  <image size=\"small\">http://userserve-ak.last.fm/serve/34/172112.jpg</image>\n\t  <image size=\"medium\">http://userserve-ak.last.fm/serve/64/172112.jpg></image>\n\t  <image size=\"large\">http://userserve-ak.last.fm/serve/126/172112.jpg</image>\n\t  <image size=\"extralarge\">http://userserve-ak.last.fm/serve/252/172112.jpg</image>\n\t  <image size=\"mega\">http://userserve-ak.last.fm/serve/500/4872357/Thievery+Corporation+thievery_corporation_2.jpg</image>\n\t
      </artist>\n
    </similar>\n    <tags>
      \n        <tag>
        \n\t  <name>electronic</name>\n\t  <url>http://www.last.fm/tag/electronic</url>\n\t
      </tag>\n        <tag>
        \n\t  <name>chillout</name>\n\t  <url>http://www.last.fm/tag/chillout</url>\n\t
      </tag>\n        <tag>
        \n\t  <name>ambient</name>\n\t  <url>http://www.last.fm/tag/ambient</url>\n\t
      </tag>\n        <tag>
        \n\t  <name>electronica</name>\n\t  <url>http://www.last.fm/tag/electronica</url>\n\t
      </tag>\n        <tag>
        \n\t  <name>french</name>\n\t  <url>http://www.last.fm/tag/french</url>\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\">French</a> 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\">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\">French</a> 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\">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;token=ADFEAEE47B16D249A97E20D7863A4DCABC7EF329D350EFC01B245F498E830260843520CB40A7C6CCB2E577B479A8B32DAE580AD9CEED469CA1&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

1 个答案:

答案 0 :(得分:0)

你是如何确定一切都在一个元素中的? doc.FirstNode == doc.LastNode?什么是doc.NodeType?

如果您只查看doc.Root.Value,那将无济于事,因为它包含所有后代文本节点的串联。

此外,您应该发布至少一小部分导致此问题的XML文档。


从您的示例中,您正在解析XML错误。 XDocument.Element获取具有给定名称的第一个子元素。 “姓名”不是该文件的子女。