1

我有这个XElement

<item>
  <guid isPermaLink="false">http://gdata.youtube.com/feeds/api/videos/s1tAYmMjLdY</guid>
  <pubDate>Wed, 17 Jun 2009 05:23:10 +0000</pubDate>
  <atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-01-29T11:28:32.000Z</atom:updated>
  <app:control xmlns:app="http://purl.org/atom/app#">
    <yt:state name="restricted" reasonCode="limitedSyndication" xmlns:yt="http://gdata.youtube.com/schemas/2007">Syndication of this video was restricted.</yt:state>
  </app:control>
  <category domain="http://schemas.google.com/g/2005#kind">http://gdata.youtube.com/schemas/2007#video</category>
  <category domain="http://gdata.youtube.com/schemas/2007/categories.cat">Music</category>
  <title>blink-182 - I Miss You</title>
  <description>Music video by blink-182 performing I Miss You. (C) 2003 Geffen Records</description>
  <link>http://www.youtube.com/watch?v=s1tAYmMjLdY&amp;feature=youtube_gdata</link>
  <author>blink182VEVO</author>
  <gd:comments xmlns:gd="http://schemas.google.com/g/2005">
    <gd:feedLink rel="http://gdata.youtube.com/schemas/2007#comments" href="http://gdata.youtube.com/feeds/api/videos/s1tAYmMjLdY/comments" countHint="46136" />
  </gd:comments>
  <media:group xmlns:media="http://search.yahoo.com/mrss/">
    <media:category label="Music" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Music</media:category>
    <media:content url="http://www.youtube.com/v/s1tAYmMjLdY?version=3&amp;f=videos&amp;app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="230" yt:format="5" xmlns:yt="http://gdata.youtube.com/schemas/2007" />
    <media:description type="plain">Music video by blink-182 performing I Miss You. (C) 2003 Geffen Records</media:description>
    <media:keywords />
    <media:player url="http://www.youtube.com/watch?v=s1tAYmMjLdY&amp;feature=youtube_gdata_player" />
    <media:restriction type="country" relationship="deny">DE</media:restriction>
    <media:thumbnail url="http://i.ytimg.com/vi/s1tAYmMjLdY/0.jpg" height="360" width="480" time="00:01:55" />
    <media:thumbnail url="http://i.ytimg.com/vi/s1tAYmMjLdY/1.jpg" height="90" width="120" time="00:00:57.500" />
    <media:thumbnail url="http://i.ytimg.com/vi/s1tAYmMjLdY/2.jpg" height="90" width="120" time="00:01:55" />
    <media:thumbnail url="http://i.ytimg.com/vi/s1tAYmMjLdY/3.jpg" height="90" width="120" time="00:02:52.500" />
    <media:title type="plain">blink-182 - I Miss You</media:title>
    <yt:duration seconds="230" xmlns:yt="http://gdata.youtube.com/schemas/2007" />
  </media:group>
  <gd:rating average="4.93231" max="5" min="1" numRaters="154927" rel="http://schemas.google.com/g/2005#overall" xmlns:gd="http://schemas.google.com/g/2005" />
  <yt:statistics favoriteCount="0" viewCount="47338118" xmlns:yt="http://gdata.youtube.com/schemas/2007" />
</item>

我想media:group和:

XElement mediaGroup = element.Element("media:group");

但我收到此错误:

The ':' character, hexadecimal value 0x3A, cannot be included in a name.

我该如何解决?

4

2 回答 2

2

您需要单独指定命名空间 - 您不能XName直接从字符串转换为非本地。LINQ to XML 使用隐式转换 from stringtoXNamespace和重载+运算符来创建XNamefrom an使这变得容易XNamespace + string

例如:

XNamespace media = "http://search.yahoo.com/mrss/";
XElement mediaGroup = element.Element(media + "group");

(您可以XName.Get按照 Cédric 的回答使用,但由于您可能在多个位置使用相同的命名空间,我更喜欢像这样拆分它。)

于 2013-01-29T12:33:43.053 回答
0

:元素名称拆分为前缀(命名空间的,此处为 : http://search.yahoo.com/mrss/)和本地名称。

使用 :XName.Get("group", "http://search.yahoo.com/mrss/")代替。

string mediaNamespace = "http://search.yahoo.com/mrss/";
XElement mediaGroup = element.Element(XName.Get("group", mediaNamespace));
于 2013-01-29T12:33:51.637 回答