Community discussion forum

RSS Feed Helper Class

This is a comment thread discussing RSS Feed Helper Class
  • 9 years ago

    This thread is for discussions of RSS Feed Helper Class.

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 4 years ago

    Very nice wrapper

  • 4 years ago

    thanks for this it helped a lot!  Here's a simple pruning function which would remove past ttl items from the rss feed once its up.  NOTE: i don't correct for time zones if specified at the end of <pubDate>, though you easily could in the for loop.


    Code:

    /// <summary>
       /// Attempts to remove items past their time to live value by opening the RSS feed as an xml document and remove the elements with expired ttl
       /// </summary>
       /// <param name="XmlFileStream">a System.IO.Stream of the feed xml</param>
       /// <returns>updated document as System.IO.Stream</returns>
       public System.IO.Stream PrunePastTtl_getStream(System.IO.Stream XmlFeed)
       {
           XmlDocument        feedDoc    = new XmlDocument();
           XmlNode            docfrag;
           XmlNodeList        nodelist;
           int                prunettl;


           DateTime        TargetPruneDt;
           TimeSpan        ttlTime;


           try
           {
               feedDoc.Load(XmlFeed);


               nodelist = feedDoc.GetElementsByTagName("channel");            
               docfrag = nodelist.Item(0);


               
               string ttlstring = docfrag.InnerXml;


               int begin = ttlstring.IndexOf("<ttl>") + 5;
               int end = ttlstring.IndexOf(@"</ttl>");


               ttlstring = ttlstring.Substring(begin,(end - begin));


               
               prunettl = Convert.ToInt32(ttlstring);
               ttlTime = new TimeSpan(prunettl,0,0,0);
               
               TargetPruneDt = DateTime.Now.Subtract(ttlTime);


               // at this point should know the ttl so process <item>s
           
               for(int i=0; i< docfrag.ChildNodes.Count; i++)
               {
                   if(docfrag.ChildNodes.Item(i).Name == "item")
                   {


                       string blobOXml = docfrag.ChildNodes.Item(i).InnerXml;
                       int pubbegin = blobOXml.IndexOf("<pubDate>") + 9;
                       int pubend = blobOXml.IndexOf("</pubDate>");
                   
                       string pubdate = blobOXml.Substring(pubbegin,(pubend - pubbegin));
                   
                       if(pubdate.IndexOf("+") != -1)
                       {
                           pubdate = pubdate.Remove(pubdate.IndexOf("+"),(pubdate.Length - pubdate.IndexOf("+")));
                       }
                       else if(pubdate.IndexOf("-") != -1)
                       {
                           pubdate = pubdate.Remove(pubdate.IndexOf("-"),(pubdate.Length - pubdate.IndexOf("-")));
                       }


                       DateTime publishDate = DateTime.Parse(pubdate);


                       if(publishDate.Ticks < TargetPruneDt.Ticks)
                       {
                           docfrag.RemoveChild(docfrag.ChildNodes.Item(i));
                           i--;  // if we remove one dont want to skip one as we iterate through
                       }
                   }
               }
           
               System.IO.Stream returnval = new System.IO.MemoryStream();
               feedDoc.Save(returnval);
               returnval.Position = 0;


                       return returnval;
           }
           catch(Exception appEx)
           {
               Exception AppEx = new ApplicationException("Unable to load correctly - " + appEx.Message,appEx);
               throw AppEx;
           }


       }

  • 1 year ago

    When I run the sample, I got this error :

     

    The XML page cannot be displayed

    Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.


    Cannot have a DOCTYPE declaration outside of a prolog. Error processing resource 'http://localhost/TestLibrary/Default.aspx...

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    ----------^

     

     

    Please anyone advise me to solve the problem, thanks in advance.

Post a reply

Enter your message below

Sign in or Join us (it's free).