1

I have this 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>

And i want to get media:group with :

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

but i get this error:

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

How can i fix it?

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
YosiFZ
  • 7,792
  • 21
  • 114
  • 221

3 Answers3

2

You need to specify the namespace separately - you can't convert from a string to a non-local XName directly. LINQ to XML makes this easy using an implicit conversion from string to XNamespace, and then an overloaded + operator to create an XName from an XNamespace + string.

For example:

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

(You can use XName.Get as per Cédric's answer, but as you're likely to use the same namespace in multiple locations, I prefer to split it out like this.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @MTA: Well yes, you then use `mediaGroup.Element(media + "title")` and either use the explicit conversion to `string`, or use the `Value` property. – Jon Skeet Jan 29 '13 at 12:51
0

The : splits the element name between the prefix (of the namespace, here : http://search.yahoo.com/mrss/) and the local name.

Use the : XName.Get("group", "http://search.yahoo.com/mrss/") instead.

string mediaNamespace = "http://search.yahoo.com/mrss/";
XElement mediaGroup = element.Element(XName.Get("group", mediaNamespace));
Cédric Bignon
  • 12,892
  • 3
  • 39
  • 51
0

Here, Just add the namespace to the xml document and add the variable to your element

XNamespace rss = "http://www.w3.org/2005/Atom";
XDocument feedXml = new(
  new XElement("rss",
    new XAttribute(XNamespace.Xmlns + "atom", rss),
    new XAttribute("Version", "2.0")
));

This would be the output:

<rss xmlns:atom="http://www.w3.org/2005/Atom" Version="2.0">
<channel>
<atom:link href="http://localhost:25291/news-and-articles?rss=Regulatory-Focus" rel="self" type="application/rss+xml"/>
Twenty
  • 5,234
  • 4
  • 32
  • 67