XElement wont return any media:content / xmlns:media breaks XElement
Here is my sample xml:
<rss version='2.0' xmlns:media='http://se开发者_JS百科arch.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)
Wrong namespace. There's no "/" at the end.
This should work:
XNamespace media = XNamespace.Get("http://search.yahoo.com/mrss/");
Also note that you have to fix it in the media:content element as well, it should say
<media:content url='http://LINK' type='' xmlns:media='http://search.yahoo.com/mrss/'/>
Should that be
XNamespace media = "{http://search.yahoo.com/mrss/}";
ie with curly braces?
精彩评论