Monday, April 18, 2016

Display an image from within an RSS Feed Asp.Net

Leave a Comment

I'm trying to display an image from an RSS Feed. At the moment I am displaying the Title, Date, and Description by using the code:

<asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1"> <ItemTemplate>     <div class="jumbotron">     <h2><%# XPath("title") %></h2><br />     <h3><%# XPath("pubDate") %></h3><br />     <h3><%# XPath("description") %></h3><br />     <a class ="btn btn-primary btn-lg" Target="_blank" href="<%# XPath("link") %>">Read More On This Story</a>         </div>     <hr /> </ItemTemplate> 

<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="http://feeds.bbci.co.uk/news/education/rss.xml" XPath="rss/channel/item"> 

After I add this image, I am looking to display each story within the page itself. To at the moment, it is opening up a new tab. I am looking to use possibly an iframe or a modal. Thanks in advance!

1 Answers

Answers 1

Here is a complete solution to showing images from your rss feed:

<asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1">   <ItemTemplate>   <div class="jumbotron">     <h2><%# XPath("title") %></h2>     <br />     <h3><%# XPath("pubDate") %></h3>     <br />     <h3><%# XPath("description") %></h3>     <br />     <asp:Repeater runat="server" ID="_subitemsRepeater"       EnableViewState="false"       DataSource='<%# XPathSelect("media:thumbnail", XmlNamespaceManager) %>'>       <ItemTemplate>         <img src="<%# ((System.Xml.XmlNode)Container.DataItem).Attributes["url"].Value %>" />         <br />       </ItemTemplate>       </asp:Repeater>       <br />       <a class="btn btn-primary btn-lg" target="_blank" href="<%# XPath("link") %>">Read More On This Story</a>       </div>       <hr /> </ItemTemplate> </asp:DataList> <asp:XmlDataSource ID="XmlDataSource1" runat="server"   DataFile="http://feeds.bbci.co.uk/news/education/rss.xml"   XPath="rss/channel/item" /> 

In code behind:

public partial class _Default : Page {   protected XmlNamespaceManager XmlNamespaceManager { get; set; }    protected void Page_Load(object sender, EventArgs e)   {      XmlNamespaceManager = new XmlNamespaceManager(XmlDataSource1.GetXmlDocument().NameTable);      XmlNamespaceManager.AddNamespace("media", "http://search.yahoo.com/mrss/");   }  } 

If your feed don't have multiple images you can replace repeater with a single image tag like this:

<img src="<%# XPath("media:thumbnail/@url", XmlNamespaceManager) %>" /> 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment