问题
I'm attempting to parse an RSS feed using C# and the SyndicationFeed class. I'm iterating over the items to get a list of SyndicationItems and that is exposing most of the feed properties, but what I cannot get out is the media:thumbnail element:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:ddn="http://discoverydn.com/about">
<channel>
<title>Revision3 Feed</title>
<link>http://revision3.com/user/1564421</link>
<description>Customized RSS Feed of all your favorite Revision3 shows.</description>
<language>en-us</language>
<itunes:explicit>no</itunes:explicit>
<itunes:summary></itunes:summary>
<itunes:subtitle></itunes:subtitle>
<itunes:author>Revision3</itunes:author>
<itunes:owner>
<itunes:email>feedback@revision3.com</itunes:email>
<itunes:name>Revision3</itunes:name>
</itunes:owner>
<itunes:image href="http://revision3.com/forum/image.php?u=415677" />
<itunes:keywords />
<image>
<title>Revision3 Feed</title>
<width>100</width>
<height>100</height>
<link>http://revision3.com/user/1564221</link>
<url>http://revision3.com/forum/image.php?u=415677</url>
</image>
<atom:link rel="self" type="application/rss+xml" title="Revision3 Feed" href="http://revision3.com/feed/user/0a7ehtg09b0ab0/" />
<item>
<title>Tekzilla Bites - Toyota Announces New Zero-Emission Fuel Cell Vehicle For 2015!</title>
<link>http://revision3.com/tekzillabites/toyota-fuel-cell</link>
<description>
<p>This year at their 2014 CES press conference, Toyota announced the Fuel Cell Vehicle (FCV): a hydrogen-powered vehicle whose only emission is water vapor going to market in 2015. Join Patrick for a look at the car, plus some of Toyota's announced specs, right from the CES show floor.
</p>
</description>
<content:encoded>
<p>This year at their 2014 CES press conference, Toyota announced the Fuel Cell Vehicle (FCV): a hydrogen-powered vehicle whose only emission is water vapor going to market in 2015. Join Patrick for a look at the car, plus some of Toyota's announced specs, right from the CES show floor.
</p>
</content:encoded>
<author>feedback@revision3.com (Revision3)</author>
<pubDate>Tue, 07 Jan 2014 07:00:00 GMT</pubDate>
<itunes:image href="http://revision3.com/forum/image.php?u=415677" />
<itunes:duration>154</itunes:duration>
<itunes:explicit>no</itunes:explicit>
<itunes:keywords>toyota, fuel cell vehicle, hydrogen electric, hydrogen electricity, fcv, fuel cell, water exhaust, electric car, ces 2014</itunes:keywords>
<itunes:author>Revision3</itunes:author>
<itunes:subtitle>
This year at their 2014 CES press conference, Toyota announced the Fuel Cell Vehicle (FCV): a hydrogen-powered vehicle whose only emission is water vapor going to market in 2015. Join Patrick for a look at the car, plus some of Toyota's announced specs,
</itunes:subtitle>
<guid isPermaLink="false">/tekzilla/tekzillabites/0025/tekzilla--tekzillabites--0025--toyota-fuel-cell--hd720p30.h264.mp4</guid>
<enclosure url="http://videos.revision3.com/revision3/web/tekzilla/tekzillabites/0025/tekzilla--tekzillabites--0025--toyota-fuel-cell--hd720p30.h264.mp4" length="27874999" type="video/mp4" />
<media:content duration="154" medium="video" fileSize="27874999" url="http://videos.revision3.com/revision3/web/tekzilla/tekzillabites/0025/tekzilla--tekzillabites--0025--toyota-fuel-cell--hd720p30.h264.mp4" type="video/mp4">
<media:title>Tekzilla Bites - Toyota Announces New Zero-Emission Fuel Cell Vehicle For 2015!</media:title>
<media:description>
<p>This year at their 2014 CES press conference, Toyota announced the Fuel Cell Vehicle (FCV): a hydrogen-powered vehicle whose only emission is water vapor going to market in 2015. Join Patrick for a look at the car, plus some of Toyota's announced specs, right from the CES show floor.
</p>
</media:description>
<media:thumbnail url="http://videos.revision3.com/revision3/images/shows/tekzilla/tekzillabites/0025/tekzilla--tekzillabites--0025--toyota-fuel-cell--marge.thumb.jpg" width="640" height="360" />
<media:thumbnail url="http://videos.revision3.com/revision3/images/shows/tekzilla/tekzillabites/0025/tekzilla--tekzillabites--0025--toyota-fuel-cell--medium.thumb.jpg" width="300" height="169" />
<media:thumbnail url="http://videos.revision3.com/revision3/images/shows/tekzilla/tekzillabites/0025/tekzilla--tekzillabites--0025--toyota-fuel-cell--mini.thumb.jpg" width="100" height="100" />
<media:player url="http://revision3.com/tekzillabites/toyota-fuel-cell" />
<media:rating>nonadult</media:rating>
<media:keywords>toyota, fuel cell vehicle, hydrogen electric, hydrogen electricity, fcv, fuel cell, water exhaust, electric car, ces 2014</media:keywords>
</media:content>
<ddn:id>28583</ddn:id>
</item>
<item>
<title>Film Riot - Mondays: Getting Recognized, Poster Credits & Royalty Free Music!</title>
<link>http://revision3.com/filmriot/poster-credits</link>
<description>
Ryan opens up the mail sack and answers some questions!
</description>
<content:encoded>
Ryan opens up the mail sack and answers some questions!
</content:encoded>
<author>feedback@revision3.com (Revision3)</author>
<pubDate>Tue, 07 Jan 2014 05:00:00 GMT</pubDate>
<itunes:image href="http://revision3.com/forum/image.php?u=415677" />
<itunes:duration>522</itunes:duration>
<itunes:explicit>no</itunes:explicit>
<itunes:keywords>getting recognized, getting yourself out there, poster credits, royalty free music, music, getting permission to use music, film riot, mail sack, questions, answers, q&a, ryan connolly</itunes:keywords>
<itunes:author>Revision3</itunes:author>
<itunes:subtitle>
Ryan opens up the mail sack and answers some questions!
</itunes:subtitle>
<guid isPermaLink="false">/filmriot/0364/filmriot--0364--poster-credits--hd720p30.h264.mp4</guid>
<enclosure url="http://videos.revision3.com/revision3/web/filmriot/0364/filmriot--0364--poster-credits--hd720p30.h264.mp4" length="73676325" type="video/mp4" />
<media:content duration="522" medium="video" fileSize="73676325" url="http://videos.revision3.com/revision3/web/filmriot/0364/filmriot--0364--poster-credits--hd720p30.h264.mp4" type="video/mp4">
<media:title>Film Riot - Mondays: Getting Recognized, Poster Credits & Royalty Free Music!</media:title>
<media:description>
Ryan opens up the mail sack and answers some questions!
</media:description>
<media:thumbnail url="http://videos.revision3.com/revision3/images/shows/filmriot/0364/filmriot--0364--poster-credits--marge.thumb.jpg" width="640" height="360" />
<media:thumbnail url="http://videos.revision3.com/revision3/images/shows/filmriot/0364/filmriot--0364--poster-credits--medium.thumb.jpg" width="300" height="169" />
<media:thumbnail url="http://videos.revision3.com/revision3/images/shows/filmriot/0364/filmriot--0364--poster-credits--mini.thumb.jpg" width="100" height="100" />
<media:player url="http://revision3.com/filmriot/poster-credits" />
<media:rating>nonadult</media:rating>
<media:keywords>getting recognized, getting yourself out there, poster credits, royalty free music, music, getting permission to use music, film riot, mail sack, questions, answers, q&a, ryan connolly</media:keywords>
</media:content>
<ddn:id>28579</ddn:id>
</item>
<item>
<title>Tekzilla Bites - Roku's Smart TV, More Channels, No Box- CES 2014</title>
<link>http://revision3.com/tekzillabites/roku-tv-ces-2014</link>
<description>
<p>Roku known for their ever popular line up of stand-alone media streaming devices, shook things up at CES 2014 this year. Enter: Roku TV, a TV with the Roku platform baked in and a few new extras that will surprise even veteran Roku users. Patrick Norton has the details. </p>
</description>
<content:encoded>
<p>Roku known for their ever popular line up of stand-alone media streaming devices, shook things up at CES 2014 this year. Enter: Roku TV, a TV with the Roku platform baked in and a few new extras that will surprise even veteran Roku users. Patrick Norton has the details. </p>
</content:encoded>
<author>feedback@revision3.com (Revision3)</author>
<pubDate>Tue, 07 Jan 2014 05:00:00 GMT</pubDate>
<itunes:image href="http://revision3.com/forum/image.php?u=415677" />
<itunes:duration>105</itunes:duration>
<itunes:explicit>no</itunes:explicit>
<itunes:keywords>roku tv, roku, tcl, hisense, ces2014, ces 2014, roku lt, ruku 1, streaming, stream, media, tekzilla, roku remote, set top box, coard cutting</itunes:keywords>
<itunes:author>Revision3</itunes:author>
<itunes:subtitle>
Roku known for their ever popular line up of stand-alone media streaming devices, shook things up at CES 2014 this year. Enter: Roku TV, a TV with the Roku platform baked in and a few new extras that will surprise even veteran Roku users. Patrick Norton
</itunes:subtitle>
<guid isPermaLink="false">/tekzilla/tekzillabites/0024/tekzilla--tekzillabites--0024--roku-tv-ces-2014--hd720p30.h264.mp4</guid>
<enclosure url="http://videos.revision3.com/revision3/web/tekzilla/tekzillabites/0024/tekzilla--tekzillabites--0024--roku-tv-ces-2014--hd720p30.h264.mp4" length="21262105" type="video/mp4" />
<media:content duration="105" medium="video" fileSize="21262105" url="http://videos.revision3.com/revision3/web/tekzilla/tekzillabites/0024/tekzilla--tekzillabites--0024--roku-tv-ces-2014--hd720p30.h264.mp4" type="video/mp4">
<media:title>Tekzilla Bites - Roku's Smart TV, More Channels, No Box- CES 2014</media:title>
<media:description>
<p>Roku known for their ever popular line up of stand-alone media streaming devices, shook things up at CES 2014 this year. Enter: Roku TV, a TV with the Roku platform baked in and a few new extras that will surprise even veteran Roku users. Patrick Norton has the details. </p>
</media:description>
<media:thumbnail url="http://videos.revision3.com/revision3/images/shows/tekzilla/tekzillabites/0024/tekzilla--tekzillabites--0024--roku-tv-ces-2014--marge.thumb.jpg" width="640" height="360" />
<media:thumbnail url="http://videos.revision3.com/revision3/images/shows/tekzilla/tekzillabites/0024/tekzilla--tekzillabites--0024--roku-tv-ces-2014--medium.thumb.jpg" width="300" height="169" />
<media:thumbnail url="http://videos.revision3.com/revision3/images/shows/tekzilla/tekzillabites/0024/tekzilla--tekzillabites--0024--roku-tv-ces-2014--mini.thumb.jpg" width="100" height="100" />
<media:player url="http://revision3.com/tekzillabites/roku-tv-ces-2014" />
<media:rating>nonadult</media:rating>
<media:keywords>roku tv, roku, tcl, hisense, ces2014, ces 2014, roku lt, ruku 1, streaming, stream, media, tekzilla, roku remote, set top box, coard cutting</media:keywords>
</media:content>
<ddn:id>28582</ddn:id>
</item>
</channel>
</rss>
The entire media:content block is not deserialized from the XML passed in. I've iterated over the entire element extensions block using
foreach (SyndicationElementExtension extension in item.ElementExtensions)
but its not in there. Is there any way I can tell the SyndicationFeed.Load() method to also parse this block for me:
SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
回答1:
Ok.. Found your data.
Try this, where the data variable is the string you've provided.
The inner XElement.Value contains the entire media block, so you will then need to query it and select which-ever element you want.
The Dump() method is an extension method available from LinqPad which is where I do my debugging.
var data = @"<?xml version=""1.0"" encoding=""utf-8""?>
<rss version=""2.0"" xmlns:itunes=""http://www.itunes.com/dtds/podcast-1.0.dtd""
xmlns:content=""http://purl.org/rss/1.0/modules/content/""
xmlns:creativeCommons=""http://backend.userland.com/creativeCommonsRssModule""
xmlns:media=""http://search.yahoo.com/mrss/""
xmlns:atom=""http://www.w3.org/2005/Atom""
xmlns:ddn=""http://discoverydn.com/about"">
<channel>
<title>Revision3 Feed</title>
<link>http://revision3.com/user/1564421</link>
<description>Customized RSS Feed of all your favorite Revision3 shows.</description>
<language>en-us</language>
<itunes:explicit>no</itunes:explicit>
<itunes:summary></itunes:summary>
<itunes:subtitle></itunes:subtitle>
<itunes:author>Revision3</itunes:author>
<itunes:owner>
<itunes:email>feedback@revision3.com</itunes:email>
<itunes:name>Revision3</itunes:name>
</itunes:owner>
<itunes:image href=""http://revision3.com/forum/image.php?u=415677"" />
<itunes:keywords />
<image>
<title>Revision3 Feed</title>
<width>100</width>
<height>100</height>
<link>http://revision3.com/user/1564221</link>
<url>http://revision3.com/forum/image.php?u=415677</url>
</image>
<atom:link rel=""self"" type=""application/rss+xml"" title=""Revision3 Feed"" href=""http://revision3.com/feed/user/0a7ehtg09b0ab0/"" />
<item>
<title>Tekzilla Bites - Toyota Announces New Zero-Emission Fuel Cell Vehicle For 2015!</title>
<link>http://revision3.com/tekzillabites/toyota-fuel-cell</link>
<description>
<p>This year at their 2014 CES press conference, Toyota announced the Fuel Cell Vehicle (FCV): a hydrogen-powered vehicle whose only emission is water vapor going to market in 2015. Join Patrick for a look at the car, plus some of Toyota's announced specs, right from the CES show floor.
</p>
</description>
<content:encoded>
<p>This year at their 2014 CES press conference, Toyota announced the Fuel Cell Vehicle (FCV): a hydrogen-powered vehicle whose only emission is water vapor going to market in 2015. Join Patrick for a look at the car, plus some of Toyota's announced specs, right from the CES show floor.
</p>
</content:encoded>
<author>feedback@revision3.com (Revision3)</author>
<pubDate>Tue, 07 Jan 2014 07:00:00 GMT</pubDate>
<itunes:image href=""http://revision3.com/forum/image.php?u=415677"" />
<itunes:duration>154</itunes:duration>
<itunes:explicit>no</itunes:explicit>
<itunes:keywords>toyota, fuel cell vehicle, hydrogen electric, hydrogen electricity, fcv, fuel cell, water exhaust, electric car, ces 2014</itunes:keywords>
<itunes:author>Revision3</itunes:author>
<itunes:subtitle>
This year at their 2014 CES press conference, Toyota announced the Fuel Cell Vehicle (FCV): a hydrogen-powered vehicle whose only emission is water vapor going to market in 2015. Join Patrick for a look at the car, plus some of Toyota's announced specs,
</itunes:subtitle>
<guid isPermaLink=""false"">/tekzilla/tekzillabites/0025/tekzilla--tekzillabites--0025--toyota-fuel-cell--hd720p30.h264.mp4</guid>
<enclosure url=""http://videos.revision3.com/revision3/web/tekzilla/tekzillabites/0025/tekzilla--tekzillabites--0025--toyota-fuel-cell--hd720p30.h264.mp4"" length=""27874999"" type=""video/mp4"" />
<media:content duration=""154"" medium=""video"" fileSize=""27874999"" url=""http://videos.revision3.com/revision3/web/tekzilla/tekzillabites/0025/tekzilla--tekzillabites--0025--toyota-fuel-cell--hd720p30.h264.mp4"" type=""video/mp4"">
<media:title>Tekzilla Bites - Toyota Announces New Zero-Emission Fuel Cell Vehicle For 2015!</media:title>
<media:description>
<p>This year at their 2014 CES press conference, Toyota announced the Fuel Cell Vehicle (FCV): a hydrogen-powered vehicle whose only emission is water vapor going to market in 2015. Join Patrick for a look at the car, plus some of Toyota's announced specs, right from the CES show floor.
</p>
</media:description>
<media:thumbnail url=""http://videos.revision3.com/revision3/images/shows/tekzilla/tekzillabites/0025/tekzilla--tekzillabites--0025--toyota-fuel-cell--marge.thumb.jpg"" width=""640"" height=""360"" />
<media:thumbnail url=""http://videos.revision3.com/revision3/images/shows/tekzilla/tekzillabites/0025/tekzilla--tekzillabites--0025--toyota-fuel-cell--medium.thumb.jpg"" width=""300"" height=""169"" />
<media:thumbnail url=""http://videos.revision3.com/revision3/images/shows/tekzilla/tekzillabites/0025/tekzilla--tekzillabites--0025--toyota-fuel-cell--mini.thumb.jpg"" width=""100"" height=""100"" />
<media:player url=""http://revision3.com/tekzillabites/toyota-fuel-cell"" />
<media:rating>nonadult</media:rating>
<media:keywords>toyota, fuel cell vehicle, hydrogen electric, hydrogen electricity, fcv, fuel cell, water exhaust, electric car, ces 2014</media:keywords>
</media:content>
<ddn:id>28583</ddn:id>
</item>
<item>
<title>Film Riot - Mondays: Getting Recognized, Poster Credits & Royalty Free Music!</title>
<link>http://revision3.com/filmriot/poster-credits</link>
<description>
Ryan opens up the mail sack and answers some questions!
</description>
<content:encoded>
Ryan opens up the mail sack and answers some questions!
</content:encoded>
<author>feedback@revision3.com (Revision3)</author>
<pubDate>Tue, 07 Jan 2014 05:00:00 GMT</pubDate>
<itunes:image href=""http://revision3.com/forum/image.php?u=415677"" />
<itunes:duration>522</itunes:duration>
<itunes:explicit>no</itunes:explicit>
<itunes:keywords>getting recognized, getting yourself out there, poster credits, royalty free music, music, getting permission to use music, film riot, mail sack, questions, answers, q&a, ryan connolly</itunes:keywords>
<itunes:author>Revision3</itunes:author>
<itunes:subtitle>
Ryan opens up the mail sack and answers some questions!
</itunes:subtitle>
<guid isPermaLink=""false"">/filmriot/0364/filmriot--0364--poster-credits--hd720p30.h264.mp4</guid>
<enclosure url=""http://videos.revision3.com/revision3/web/filmriot/0364/filmriot--0364--poster-credits--hd720p30.h264.mp4"" length=""73676325"" type=""video/mp4"" />
<media:content duration=""522"" medium=""video"" fileSize=""73676325"" url=""http://videos.revision3.com/revision3/web/filmriot/0364/filmriot--0364--poster-credits--hd720p30.h264.mp4"" type=""video/mp4"">
<media:title>Film Riot - Mondays: Getting Recognized, Poster Credits & Royalty Free Music!</media:title>
<media:description>
Ryan opens up the mail sack and answers some questions!
</media:description>
<media:thumbnail url=""http://videos.revision3.com/revision3/images/shows/filmriot/0364/filmriot--0364--poster-credits--marge.thumb.jpg"" width=""640"" height=""360"" />
<media:thumbnail url=""http://videos.revision3.com/revision3/images/shows/filmriot/0364/filmriot--0364--poster-credits--medium.thumb.jpg"" width=""300"" height=""169"" />
<media:thumbnail url=""http://videos.revision3.com/revision3/images/shows/filmriot/0364/filmriot--0364--poster-credits--mini.thumb.jpg"" width=""100"" height=""100"" />
<media:player url=""http://revision3.com/filmriot/poster-credits"" />
<media:rating>nonadult</media:rating>
<media:keywords>getting recognized, getting yourself out there, poster credits, royalty free music, music, getting permission to use music, film riot, mail sack, questions, answers, q&a, ryan connolly</media:keywords>
</media:content>
<ddn:id>28579</ddn:id>
</item>
<item>
<title>Tekzilla Bites - Roku's Smart TV, More Channels, No Box- CES 2014</title>
<link>http://revision3.com/tekzillabites/roku-tv-ces-2014</link>
<description>
<p>Roku known for their ever popular line up of stand-alone media streaming devices, shook things up at CES 2014 this year. Enter: Roku TV, a TV with the Roku platform baked in and a few new extras that will surprise even veteran Roku users. Patrick Norton has the details. </p>
</description>
<content:encoded>
<p>Roku known for their ever popular line up of stand-alone media streaming devices, shook things up at CES 2014 this year. Enter: Roku TV, a TV with the Roku platform baked in and a few new extras that will surprise even veteran Roku users. Patrick Norton has the details. </p>
</content:encoded>
<author>feedback@revision3.com (Revision3)</author>
<pubDate>Tue, 07 Jan 2014 05:00:00 GMT</pubDate>
<itunes:image href=""http://revision3.com/forum/image.php?u=415677"" />
<itunes:duration>105</itunes:duration>
<itunes:explicit>no</itunes:explicit>
<itunes:keywords>roku tv, roku, tcl, hisense, ces2014, ces 2014, roku lt, ruku 1, streaming, stream, media, tekzilla, roku remote, set top box, coard cutting</itunes:keywords>
<itunes:author>Revision3</itunes:author>
<itunes:subtitle>
Roku known for their ever popular line up of stand-alone media streaming devices, shook things up at CES 2014 this year. Enter: Roku TV, a TV with the Roku platform baked in and a few new extras that will surprise even veteran Roku users. Patrick Norton
</itunes:subtitle>
<guid isPermaLink=""false"">/tekzilla/tekzillabites/0024/tekzilla--tekzillabites--0024--roku-tv-ces-2014--hd720p30.h264.mp4</guid>
<enclosure url=""http://videos.revision3.com/revision3/web/tekzilla/tekzillabites/0024/tekzilla--tekzillabites--0024--roku-tv-ces-2014--hd720p30.h264.mp4"" length=""21262105"" type=""video/mp4"" />
<media:content duration=""105"" medium=""video"" fileSize=""21262105"" url=""http://videos.revision3.com/revision3/web/tekzilla/tekzillabites/0024/tekzilla--tekzillabites--0024--roku-tv-ces-2014--hd720p30.h264.mp4"" type=""video/mp4"">
<media:title>Tekzilla Bites - Roku's Smart TV, More Channels, No Box- CES 2014</media:title>
<media:description>
<p>Roku known for their ever popular line up of stand-alone media streaming devices, shook things up at CES 2014 this year. Enter: Roku TV, a TV with the Roku platform baked in and a few new extras that will surprise even veteran Roku users. Patrick Norton has the details. </p>
</media:description>
<media:thumbnail url=""http://videos.revision3.com/revision3/images/shows/tekzilla/tekzillabites/0024/tekzilla--tekzillabites--0024--roku-tv-ces-2014--marge.thumb.jpg"" width=""640"" height=""360"" />
<media:thumbnail url=""http://videos.revision3.com/revision3/images/shows/tekzilla/tekzillabites/0024/tekzilla--tekzillabites--0024--roku-tv-ces-2014--medium.thumb.jpg"" width=""300"" height=""169"" />
<media:thumbnail url=""http://videos.revision3.com/revision3/images/shows/tekzilla/tekzillabites/0024/tekzilla--tekzillabites--0024--roku-tv-ces-2014--mini.thumb.jpg"" width=""100"" height=""100"" />
<media:player url=""http://revision3.com/tekzillabites/roku-tv-ces-2014"" />
<media:rating>nonadult</media:rating>
<media:keywords>roku tv, roku, tcl, hisense, ces2014, ces 2014, roku lt, ruku 1, streaming, stream, media, tekzilla, roku remote, set top box, coard cutting</media:keywords>
</media:content>
<ddn:id>28582</ddn:id>
</item>
</channel>
</rss>";
var rss = new Rss20FeedFormatter();
rss.ReadFrom(XmlReader.Create(new StringReader(data)));
var elements = rss.Feed.Items.SelectMany(s => s.ElementExtensions.Select(x => x.GetObject<XElement>().Value));
elements.Dump();
Another SO answer which might answer your question can be found here: Reading non-standard elements in a SyndicationItem with SyndicationFeed.
Cheers, Aaron
回答2:
You can check our MediaRssSharp for parsing media rss feeds. MediaRssSharp does most of the mrss format parsing for you including optional/non-optional elements.
回答3:
I added an extension method for the SyndicationItem class. Just create a class called 'ExtensionMethods.cs' in the root of your project and use the code below. Be sure to change the namespace to match that of your project.
using System;
namespace RFAnnouncer
{
public static class ExtensionMethods
{
public static string GetEnclosureUri(this System.ServiceModel.Syndication.SyndicationItem item)
{
for (int i = 0; i < item.Links.Count; i++)
{
if (item.Links[i].RelationshipType == "enclosure")
{
return item.Links[i].Uri.AbsoluteUri;
}
}
return "";
}
}
}
回答4:
This is my solution. I return the first thumbnail image.
// Read the feed
while (await feedReader.Read())
{
if (feedReader.ElementType == SyndicationElementType.Item)
{
// Read the item as generic content
ISyndicationContent content = await feedReader.ReadContent();
// Parse the item if needed (unrecognized tags aren't available)
// Utilize the existing parser
ISyndicationItem item = parser.CreateItem(content);
string urlImage = GetThumbailsImage(content, url);
rssNewsItems.Add(item.ConvertToNewsItem(urlImage));
}
}
private string GetThumbailsImage(ISyndicationContent content, string source)
{
// Get <media:thumbnail> field
ISyndicationContent customElement = content.Fields.FirstOrDefault(f => f.Name == "thumbnail");
var thimbUrl = customElement.Attributes.FirstOrDefault(a => a.Name == "url");
return thimbUrl?.Value;
}
来源:https://stackoverflow.com/questions/20969986/get-media-elements-from-rss-using-syndicationfeed