问题
I am attempting to consume my Zenfolio RSS feed so that i can display the images in the feed. How can i access the url value of the media:thumbnail and media:content elements in my RSS feed? I have googled high and low and not found an answer regarding how to access the url value. There was a similiar unanswered SO post.
Examples of the elements:
<media:thumbnail url="http://riderdesign.net/img/s11/v35/p449020235-2.jpg"
width="400"
height="225"
/>
<media:content url="http://riderdesign.net/img/s11/v35/p449020235-2.jpg"
type="image/jpeg" medium="image"
width="400"
height="225"
/>
My code in my controller:
Public Function Feed() As ActionResult
Dim feedurl As String = "http://riderdesign.net/recent.rss"
Using x = XmlReader.Create(feedurl)
Dim r As SyndicationFeed = SyndicationFeed.Load(x)
Return View(r)
End Using
End Function
In my view i have @ModelType System.ServiceModel.Syndication.SyndicationFeed and
@For Each i In ViewData.Model.Items
@i.Title.text @<br />
<!--What do i do here to get the url values?-->
Next
回答1:
I have my solution. I'll post code here in a bit.
Code:
Public Function Feed() As ActionResult
Dim feedurl As String = "http://riderdesign.net/p319394411/recent.rss"
Using x = XmlReader.Create(feedurl)
Dim r = XDocument.Load(x)
Dim mediapfx As XNamespace = "http://search.yahoo.com/mrss/"
Dim ml = From item In r.Descendants(mediapfx + "content") Select item
Dim medialist = From item In r.Descendants("item") Select New MediaImage With {
.Id = item.Element("guid").Value, .ImageUrl = TryGetAttributeValue(item.Element(mediapfx + "content"), "url")} Take 5
Return View(medialist)
End Using
End Function
Private Function TryGetAttributeValue(ByVal xe As XElement, ByVal attribute As String) As String
If xe IsNot Nothing AndAlso xe.Attribute(attribute) IsNot Nothing Then
Return xe.Attribute(attribute).Value
Else
Return Nothing
End If
End Function
Namespace RiderDesignMvcBlog.Core.ViewModels
Public Class MediaImage
Public Property Id() As String
Public Property ImageUrl() As String
End Class
End Namespace
In view:
@ModelType IEnumerable(Of RiderDesignMvcBlog.Core.ViewModels.MediaImage)
@Code
ViewData("Title") = "Feed"
Layout = "~/Views/Shared/_Layout4.vbhtml"
End Code
<h2>Feed</h2>
@For Each i In Model
@<img src=" @i.ImageUrl" />
Next
来源:https://stackoverflow.com/questions/8220148/how-to-access-url-attr-of-mediathumbnail-and-mediacontent-elements-in-rss-feed