locked
read XML file (RSS), but only 5 recent posts RRS feed

  • Question

  • User-1496281956 posted

    I got an RSS file with a usual strucutre like this;

    <item>
    <title></title>
    <description></description>
    <link></link>
    <pubDate></pubDate>
    </item>

    I want to get the values of title, des, link and I want the posts organised the by published date. I got this code, but does not work as expected.

    XmlReaderSettings settings = new XmlReaderSettings();
    
        settings.IgnoreComments = true;
        settings.IgnoreProcessingInstructions = true;
        settings.IgnoreWhitespace = true;
        
        XmlReader reader = XmlReader.Create(Server.MapPath("~/rss.xml"), settings);
    
        while (reader.Read())
        {
    
            if (reader.NodeType == XmlNodeType.Element && reader.Name == "item")
            {
                while (reader.NodeType != XmlNodeType.EndElement)
                {
                    reader.Read();
                    if (reader.Name == "title")
                    {
                        while (reader.NodeType != XmlNodeType.EndElement)
                        {
                            reader.Read();
                            if (reader.NodeType == XmlNodeType.Text)
                            {
                              Response.Write(reader.Value + "<br/>");
                            }
                        }
                    }
    
                   
                }
    
            }//END IF
    
        }


    There must be a better way where I can use foreachs statement to get the node innertext..?

    Saturday, August 10, 2013 3:18 PM

Answers

  • User-821857111 posted

    You are better off using Linq to XML. First, create a class as a container for each feed item:

    using System;
    public class RssItem
    {
        public string Title {get;set;}
        public string Description {get;set;}
        public string Link {get;set;}
        public DateTime PubDate {get;set;}
    }

    Then you can load the feed into an XDocument object and query it:

    @using System.Xml.Linq;
    @{
        var rss = new List<RssItem>();
        var feed = XDocument.Load("http://localhost:34321/GenerateXml.cshtml");
        var items = feed.Descendants("item");
        foreach(var item in items){
            rss.Add(new RssItem{
                Title = (string)item.Element("title"),
                Description = (string)item.Element("description"),
                Link = (string)item.Element("link"),
                PubDate = (DateTime)item.Element("pubDate")
            });
        }
        var mostRecent = rss.OrderByDescending(r => r.PubDate).Take(5);
    }




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 10, 2013 5:47 PM

All replies

  • User-821857111 posted

    You are better off using Linq to XML. First, create a class as a container for each feed item:

    using System;
    public class RssItem
    {
        public string Title {get;set;}
        public string Description {get;set;}
        public string Link {get;set;}
        public DateTime PubDate {get;set;}
    }

    Then you can load the feed into an XDocument object and query it:

    @using System.Xml.Linq;
    @{
        var rss = new List<RssItem>();
        var feed = XDocument.Load("http://localhost:34321/GenerateXml.cshtml");
        var items = feed.Descendants("item");
        foreach(var item in items){
            rss.Add(new RssItem{
                Title = (string)item.Element("title"),
                Description = (string)item.Element("description"),
                Link = (string)item.Element("link"),
                PubDate = (DateTime)item.Element("pubDate")
            });
        }
        var mostRecent = rss.OrderByDescending(r => r.PubDate).Take(5);
    }




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 10, 2013 5:47 PM
  • User1526116210 posted

    I have used Argotic Syndication Framework to achieve this in one of my projects. It was very easy to setup, first install it by searching nuget for 'argotic' and installing the core package which also installs the dependencies. Then all you need are a few lines to do what you want. This is what I achieved in my case:

    @using Argotic.Syndication;
    @{
        RssFeed feed = RssFeed.Create(new Uri("URL TO RSS HERE"));
    }
    <ul class="newsSide">
    @foreach(var item in feed.Channel.Items)
    {
      String title = item.Title;        //RSS item Title
      String desc = item.Description;  //RSS item Description
      var link = item.Link; //RSS item Link
    	<text><li><a href="@link" title="@title">@title</a> <p class="desc">@desc</p></li></text>
    }
    </ul>

    It is automatically organised by date

    Sunday, August 11, 2013 7:43 AM