Search code examples
c#rssxml-namespacesxelement

XElement wont return any media:content / xmlns:media breaks XElement


Here is my sample xml:

<rss version='2.0' xmlns:media='http://search.yahoo.com/mrss/'> 
<channel> 
    <title>Title of RSS feed</title> 
    <link>LINK</link> 
    <description>Details about the feed</description> 
    <language>en</language> 
    <item>
      <title>TITLE</title>
      <media:content url='http://LINK' type='' xmlns:media='http://search.yahoo.com/mrss' />
    </item>
</channel>
</rss>

My Code:

XElement rss = XElement.Parse(xml);
XNamespace media = "http://search.yahoo.com/mrss/";

var item = rss.Element("channel").Elements("item").First();
var mediaa = item.Element(media + "content"); //this part doesn't work as expected
var url = mediaa.Attribute("url");

it seems as if the "xmlns:media='http://search.yahoo.com/mrss'" part of the media:content tag is breaking the .Element(media + "content"). (I can not change the feed)


Solution

  • Wrong namespace. There's no "/" at the end.