Reputation: 15478
I've got xml that looks like what I have below. I can read the title but am having trouble getting to the url of media:content. Any suggestions? My non-working c# is below along with the xml.
XNamespace xmlns = "http://www.w3.org/2005/Atom";
var names =
(from data in XDocument.Load("http://channel9.msdn.com/Events/Build/2012/rss").Descendants("item")
let xElement = data.Element("title")
let xElementUrls = data.Element("media")
where xElement != null
select new
{
Title = xElement.Value,
Urls = data.Elements(xmlns + "media:group")
//MediaGroup = data.Element("media:group")
}).ToList();
and the XML:
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
xmlns:c9="http://channel9.msdn.com">
<channel>
<item>
<title>Building Windows 8 LOB Apps (Repeat)</title>
<media:group>
<media:content url="http://video.ch9.ms/sessions/build/2012/2-104R.mp4"
expression="full" duration="0" fileSize="1" type="video/mp4" medium="video"/>
<media:content url="http://video.ch9.ms/sessions/build/2012/2-104R.wmv"
expression="full" duration="0" fileSize="1" type="video/x-ms-wmv" medium="video"/>
</media:group>
</item>
<item>
<item>
...
</item>
Added this based on L.B.'s suggestion but I can't figure out how to get url's out (it is now returning a list of URL's per item as expected.
var items = xDoc.Descendants("item")
.Where(g => g.Element(media + "group") != null)
.Select(g => new {
Title = g.Element("title").Value,
Url = g.Element(media + "group")
.Element(media + "content")
.Attribute("url").Value
})
.ToList();
Upvotes: 2
Views: 803
Reputation: 116108
var xDoc = XDocument.Load("http://channel9.msdn.com/Events/Build/2012/rss");
XNamespace media = "http://search.yahoo.com/mrss/";
var items = xDoc.Descendants("item")
.Where(g => g.Element(media + "group") != null)
.Select(g => new {
Title = g.Element("title").Value,
Url = g.Element(media + "group")
.Element(media + "content")
.Attribute("url").Value
})
.ToList();
Upvotes: 2
Reputation: 8181
You have the wrong namespace...
XNamespace xmlns = "http://www.w3.org/2005/Atom";
should be
XNamespace xmlns = "http://search.yahoo.com/mrss/";
And you are combining the namespace and element name incorrectly
...
Urls = data.Elements(xmlns + "group")
....
Upvotes: 1
Reputation: 887255
media
is a namespace alias, not an element.
You need to get the group
element within the http://search.yahoo.com/mrss/
namespace:
XNamespace m = "http://search.yahoo.com/mrss/";
let xElementUrls = data.Element(m + "group")
Upvotes: 1