Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • JJ 1 post 71 karma points
    Oct 06, 2017 @ 14:20
    JJ
    0

    Syntax Question about importing an rss feed

    Hey guys,

    Love Umbraco, but am not very well versed in all things cshtml.

    I'm trying to do something really simple, import and render an rss feed. Here's what I have so far that works well

            <div class="rssFeed">    
            @using System.Xml.XPath;
            @using System.Xml;
    
            @{
                XmlTextReader udBrudRSS = new XmlTextReader(RssFeedDrillDown);
                XmlDocument doc = new XmlDocument();
                doc.Load(udBrudRSS);
                XmlNodeList rssItems = doc.SelectNodes("//item");
            }
            <ul class="rssFeed">
                @{
    
                    int someLinkCount = Int32.Parse(Model.GetElementValue("FeedItems"));
                    var orig = "Wed, 29 Oct 2008 14:14:48 +0000".AsDateTime().ToString("d");
                    var i =0;
                    foreach (XmlNode node in rssItems)
                    {
                        <li class="eventVertGrid">
                        <h4 class="eventHeader lightBlue serifFont"><span>@node["pubDate"].InnerText.AsDateTime().ToString("d")</span></h4>
                          @{<p class="rssMessage" style="font-weight: bold;">@Html.Raw(@node["description"].InnerText)</p>}
    
                        <a class="gray" href="@node["link"].InnerText">@node["title"].InnerText </a>                   
                        </li>
                        i++;
                        if (i == @someLinkCount){ break; }
                    }
                }
            </ul>
            </div>
    

    The one thing that I cannot figure out is rendering an image url from a media:content tag in the rss feed.

    To give an idea of what i'm TRYING to do, here's a rough iteration of what I'm been spinning my tires on:

       <img src="@node["media:content"].Attributes["url"]" />
    

    Am I way off here?

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Oct 07, 2017 @ 16:36
    Marc Goodson
    0

    Hi JJ

    Would have to see your source xml feed to be sure, and perhaps your error but...

    ... one thing to be aware of is that media:content is the content item from the media namespace declared at top of your rssfeed, and so if memory serves me correct you have to declare this same namespace in order to read the value...

    https://www.wired.com/2010/02/usemediarss/

    so my guess is you need to add this namespace after you load your document: eg:

    doc.Load(udBrudRSS);
    XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
    xnm.AddNamespace("media", "http://search.yahoo.com/mrss/");
    XmlNodeList rssItems = doc.SelectNodes("//item");
    

    Then according to this:

    https://msdn.microsoft.com/en-us/library/h0hw012b(v=vs.110).aspx

    You should then be able to select the media:content node like so:

    @{
    var mediaContent = node.SelectSingleNode("media:content",xnm);
    var urlAttribute = mediaContent.Attributes["url"];
    var url = urlAttribute !=null ? urlAttribute.Value : "";
    }
    

    but it is ages since I've done anything like this, because .net now has a SyndicationFeed class designed to read in RSS Feeds, which is normally the approach I use, although you still need to think about the namespaces

    https://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed(v=vs.110).aspx

    I've built a property editor for Umbraco, where it uses this approach to read an RSS feed Url entered into the backoffice... there is a blogpost here about it: http://tooorangey.co.uk/posts/feed-me-with-your-rss/ the code is on github https://github.com/marcemarc/tooorangey.RssFeedUrl

    Anyway hopefully my vague recollection of namespaces being the problem gets you on the right google path to sorting this out! even if my suggested lines of code aren't exactly perfect!

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft