+1-888-365-2779
Try Now
More in this section

Forums / Designing with Sitefinity / Having an issue with custom RSS control

Having an issue with custom RSS control

5 posts, 0 answered
  1. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    29 Jun 2009
    Link to this post
    I wrote a custom RSS control, which is really the same as the stock Rss Feed control. The reason I need a custom one is the RSS feed I am trying to read from my company's news site has a different structure than most RSS feeds. The one major difference is the date field is named 'date' rather than 'pubDate', so when the reader would try to parse the link, I would get this error: 

    DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'pubDate'. 

    So in my design I made the following change:

    <asp:Repeater ID="RssRepeater" Visible="false" EnableViewState="false" runat="server"
        <ItemTemplate> 
            <h2> 
                <asp:HyperLink ID="link" NavigateUrl='<%# DataBinder.Eval (Container.DataItem, "link") %>' 
                    EnableViewState="false" runat="server"><%# DataBinder.Eval (Container.DataItem, "title") %></asp:HyperLink></h2
            <div> 
                <%--<asp:Literal ID="pubDate" Text='<%# DataBinder.Eval (Container.DataItem, "pubDate") %>' 
                    Visible="false" EnableViewState="false" runat="server" /></div>--%> 
                    <asp:Literal ID="pubDate" Text='<%# DataBinder.Eval (Container.DataItem, "date") %>' 
                    Visible="false" EnableViewState="false" runat="server" /></div
            <p> 
                <asp:Literal ID="description" Text='<%# DataBinder.Eval (Container.DataItem, "description") %>' 
                    EnableViewState="false" runat="server" /></p
        </ItemTemplate> 
        <SeparatorTemplate> 
            <hr /> 
        </SeparatorTemplate> 
    </asp:Repeater> 

    As you can see, all I have done is change the pubDate literal control's DataBinder.Eval from 'pubDate' to 'date'. Now when I try to run the module, I get a NullReferenceException from line 8:

    1 protected void RssRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    2
    3     // Execute the following logic for Items and Alternating Items.   
    4     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
    5     { 
    6         // The RSS 'pubDate' is an optional element.  If we don't have it, don't display it.   
    7         Literal _pubDate = (Literal)e.Item.FindControl("pubDate"); 
    8         if (_pubDate.Text != "") 
    9         { 
    10             _pubDate.Text = FormatDate(_pubDate.Text); 
    11             _pubDate.Visible = true
    12         } 
    13     } 
    14

    I must be missing something simple because no matter what I do, _pubDate always comes up as null. I also tried throwing in a different RSS url (one that works in the stock Rss Feed control), http://feeds.digg.com/digg/popular.rss, the same error is thrown. I did revert my change in the design back to see if that was the problem, but no luck.

    Any ideas? Thanks!

    anders
  2. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    29 Jun 2009
    Link to this post
    Hmm, this is very strange. It just started working all of a sudden! I hope these errors were just a fluke haha.

    Thanks!
  3. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    29 Jun 2009
    Link to this post
    One more thing:

    My original implementation of this Feed on our old site only showed posts from the last month. What would be the easiest way to accomplish this with my control?

    This was the section of code that did the date determination in my old implementation:

    if (DateDiff(DateInterval.Month, gsHN.PostDate, Today) <= 1) { 
        oRet.Add(gsHN); 

  4. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    29 Jun 2009
    Link to this post
    Here is the solution I came up with:

    private void FetchRSS() 
        // Open an XML reader & fetch the RSS data from the RssUrl.   
        XmlTextReader reader = new XmlTextReader(RssUrl); 
        DataSet ds = new DataSet(); 
        DataTable dt = new DataTable(); 
        ds.ReadXml(reader); 
     
        //dt = ds.Tables[2].Clone; 
        dt = ds.Tables[2]; 
     
        DataTable thisIsTheDS = FilterDates(dt); 
     
        if (reader == null
        { 
            ErrorPanel.Visible = true
        } 
        else 
        { 
            RssRepeater.Visible = true
            RssRepeater.ItemDataBound += new RepeaterItemEventHandler(RssRepeater_ItemDataBound); 
            RssRepeater.DataSource = thisIsTheDS; //ds.Tables[2]; 
            RssRepeater.DataBind(); 
        } 
     
    private DataTable FilterDates(DataTable oDT) 
        DataTable oRet = new DataTable(); 
        oRet = oDT.Clone(); 
     
        try 
        { 
            DateTime dtThen; 
            DateTime dtNow = DateTime.Now; 
            TimeSpan oTS; 
     
            foreach (DataRow dr in oDT.Rows) 
            { 
                dtThen = ParseDT((string)dr.ItemArray[3]); 
                oTS = dtNow - dtThen; 
                if (oTS.Days <= DayLimit) 
                { 
                    oRet.ImportRow(dr); 
                } 
            } 
        } 
        catch 
        { 
        } 
         
        return oRet; 
     
    private DateTime ParseDT(string entry) 
        char[] splitBy = new char[] { '-' }; 
     
        string[] oSplit = entry.Split(splitBy, System.StringSplitOptions.RemoveEmptyEntries); 
     
        int oYear = int.Parse(oSplit[0]); 
        int oMonth = int.Parse(oSplit[1]); 
        int oDay = int.Parse(oSplit[2]); 
         
        DateTime oRet = new DateTime(oYear,oMonth,oDay); 
     
        return oRet; 

    Not too shabby for a C# n00b huh? haha.

    Any suggestions to improve this code are more than welcome!

    Regards,
    anders
  5. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    02 Jul 2009
    Link to this post
    Hello andersleet,

    It is perfectly fine. Perhaps the date filtering can be optimized. If you use RadGrid, you will be able to filter the dates in a better way, but this is just a suggestion.

    All the best,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Register for webinar
5 posts, 0 answered