locked
Problem with RSS Feeds RRS feed

  • Question

  • Hi there,

    I'm trying to create an app for my techblog, using Windows Phone App Studio of course. Actually it's an RSS reader. I tried to create that reader but on the phone it doesn't show the whole article - just the first 4-5 lines and then "[...]". The default RSS feed that shows Microsofts news seems to work fine (you can read the whole post). Do you know if I can fix this problem? Posts are in Greek...


    Thursday, February 20, 2014 7:19 PM

All replies

  • I guess you have to manually edit the format of text being displayed

    Hope this code helps..

    class Class1
        {
            public ObservableCollection<RSSItem> FeedItems = new ObservableCollection<RSSItem>();
            
            void method1()
            {
                WebClient wc = new WebClient();
                wc.DownloadStringCompleted += wc_DownloadStringCompleted;
                wc.DownloadStringAsync(new Uri(_feedUrl), UriKind.Absolute);
            }
    
            private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                try
                {
                    XmlReader reader = XmlReader.Create(GenerateStreamFromString(e.Result));
                    SyndicationFeed rssFeed = SyndicationFeed.Load(reader);
                    var items = from p in rssFeed.Items
                                select new RSSItem
                                {
                                    Title = p.Title.Text,
                                    Summary = FormatSummary(p).Trim(),
                                    Permalink = (p.Links.FirstOrDefault() != null) ? p.Links.FirstOrDefault().GetAbsoluteUri() : null,
                                    PublishDate = p.PublishDate.LocalDateTime.ToString(),
                                    ImageUrl = UpdateFeedImageUrl(p)
                                };
                    FeedItems = items.ToList();
                }
                catch (Exception ex)
                {
                }
            }
    
            public Stream GenerateStreamFromString(string s)
            {
                MemoryStream stream = new MemoryStream();
                StreamWriter writer = new StreamWriter(stream);
                writer.Write(s);
                writer.Flush();
                stream.Position = 0;
                return stream;
            }
    
            /// <summary>
            /// Trim the html tags
            /// </summary>
            /// <param name="p"></param>
            /// <returns></returns>
            private string FormatSummary(SyndicationItem p)
            {
                const string HTML_TAG_PATTERN = "<.*?>";
                if (!String.IsNullOrEmpty(p.Summary.Text))
                    return Regex.Replace(p.Summary.Text, HTML_TAG_PATTERN, string.Empty);
                else
                    return string.Empty;
            }
    
            private Uri UpdateFeedImageUrl(SyndicationItem p)
            {
                if (p.Links != null)
                {
                    foreach (SyndicationLink enclosure in p.Links.Where<SyndicationLink>(x => x.RelationshipType == "enclosure"))
                    {
                        if (enclosure.MediaType == "image/jpeg")
                        {
                            return enclosure.Uri; ;
                        }
                    }
                }
                return null;
            }
    
    
        }
    
    
        public class RSSItem
        {
            public System.Uri ImageUrl { get; set; }
            public System.Uri Permalink { get; set; }
            public string PublishDate { get; set; }
            public string Summary { get; set; }
            public string Title { get; set; }
        }

    Friday, February 21, 2014 7:23 AM
  • Thanks for the reply!

    That means I have to open VS Express for Windows Phone, which requires Windows 8 64-bit, right? My only Windows PC is the Asus T100 that runs 32-bit Windows... I have to figure out this problem now. :p
    Friday, February 21, 2014 8:36 AM
  • Yes, if you really wanted to work around on WP8 apps...

    :)

    Friday, February 21, 2014 8:43 AM
  • I am really new here and have had this issue forever. How exactly would I go about implementing this code??
    Tuesday, February 25, 2014 4:47 PM
  • After you do generate an app from App Studio.....

    you'll be able to download the source code from it or else, the xap file itself if you directly wanted to publish the app......

    After that you can edit whatever the bits and pieces according to your wish using the VS :)

    Does it answer your question Hakeem?

    Tuesday, February 25, 2014 4:50 PM
  • I have already downloaded the source code and opened it in VS. I meant where would I paste the code above.
    Tuesday, February 25, 2014 5:40 PM
  • I guess according to the new update, it the code should be pasted in the RssReader.cs

    AppStudioData > DataProviders > RssReaders > RssReader.cs

    Wednesday, February 26, 2014 2:54 AM
  • please am a novice where do i place it on that file, before and  after what... please kindly state where on the page, so it doesn't give error

    better days ahead...........G

    Wednesday, February 26, 2014 12:38 PM
  • I haven't done any apps for RSS after the update...I just did some for YouTube channels and flicker...

    So I dunno exactly where it should plugin....I founded this in my one..

        public async Task<IEnumerable<RssSchema>> Load()
            {
                string xmlContent = await DownloadAsync(_uri);
    
                var doc = XDocument.Parse(xmlContent);
                if (doc.Declaration.Encoding != "UTF-8")
                {
                    base.Encoding = Encoding.GetEncoding(doc.Declaration.Encoding);
                    xmlContent = await DownloadAsync(_uri);
                    doc = XDocument.Parse(xmlContent);
                }
    
                var type = BaseRssReader.GetFeedType(doc);
    
                BaseRssReader rssReader;
                if (type == RssType.Rss)
                    rssReader = new RssReader();
                else
                    rssReader = new AtomReader();
    
                return rssReader.LoadFeed(doc).Take(20);
            }

    If you do find something like this in your RssReader class...then replace with this.

     try
                {
                    XmlReader reader = XmlReader.Create(GenerateStreamFromString(e.Result));
                    SyndicationFeed rssFeed = SyndicationFeed.Load(reader);
                    var items = from p in rssFeed.Items
                                select new RSSItem
                                {
                                    Title = p.Title.Text,
                                    Summary = FormatSummary(p).Trim(),
                                    Permalink = (p.Links.FirstOrDefault() != null) ? p.Links.FirstOrDefault().GetAbsoluteUri() : null,
                                    PublishDate = p.PublishDate.LocalDateTime.ToString(),
                                    ImageUrl = UpdateFeedImageUrl(p)
                                };
                    FeedItems = items.ToList();
                }
                catch (Exception ex)
                {
                }
    

    Wednesday, February 26, 2014 12:50 PM
  • thanks mate, but this is what i find in my RssReader.cs

    using System;
    using System.Linq;
    using System.Collections.ObjectModel;
    using System.Xml.Linq;

    namespace AppStudio.Data
    {
        /// <summary>`
        /// Rss reader implementation to parse Rss content.
        /// </summary>
        internal class RssReader : BaseRssReader
        {
            private readonly XNamespace NsRdfNamespaceUri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
            private readonly XNamespace NsRdfElementsNamespaceUri = "http://purl.org/dc/elements/1.1/";

            /// <summary>
            /// THis override load and parses the document and return a list of RssSchema values.
            /// </summary>
            /// <param name="doc"></param>
            /// <returns></returns>
            public override ObservableCollection<RssSchema> LoadFeed(XDocument doc)
            {
                bool isRDF = false;
                var feed = new ObservableCollection<RssSchema>();
                XNamespace defaultNamespace = string.Empty;

                if (doc.Root != null)
                {
                    isRDF = doc.Root.Name == (NsRdfNamespaceUri + "RDF");
                    defaultNamespace = doc.Root.GetDefaultNamespace();
                }

                foreach (var item in doc.Descendants(defaultNamespace + "item"))
                {
                    var rssItem = isRDF ? ParseRDFItem(item) : ParseRssItem(item);
                    feed.Add(rssItem);
                }
                return feed;
            }

            /// <summary>
            /// RSS all versions
            /// </summary>
            /// <param name="item"></param>
            /// <returns></returns>
            private static RssSchema ParseItem(XElement item)
            {
                var rssItem = new RssSchema();
                rssItem.Title = item.GetSafeElementString("title").Trim();
                rssItem.FeedUrl = item.GetSafeElementString("link");

                string description = item.GetSafeElementString("description");
                if (string.IsNullOrEmpty(description))
                    description = item.GetSafeElementString("content");

                rssItem.Summary = Utility.DecodeHtml(description).Trim().Truncate(500, true);
                rssItem.Summary = RssHelper.SanitizeString(rssItem.Summary);
                rssItem.Content = RssHelper.SanitizeString(description);

                return rssItem;
            }

            /// <summary>
            /// RSS version 1.0
            /// </summary>
            /// <param name="item"></param>
            /// <returns></returns>
            private RssSchema ParseRDFItem(XElement item)
            {
                var rssItem = ParseItem(item);

                rssItem.PublishDate = item.GetSafeElementDate("date", NsRdfElementsNamespaceUri);

                string image = item.GetSafeElementString("image");
                if (string.IsNullOrEmpty(image))
                {
                    image = item.GetImage();
                }
                if (string.IsNullOrEmpty(image) && item.ToString().Contains("media:thumbnail"))
                {
                    XNamespace ns = "http://search.yahoo.com/mrss/";
                    var element = item.Elements(ns + "thumbnail").Last();
                    image = element.Attribute("url").Value;
                }
                if (string.IsNullOrEmpty(image) && item.ToString().Contains("thumbnail"))
                {
                    image = item.GetSafeElementString("thumbnail");
                }

                rssItem.ImageUrl = image;

                return rssItem;
            }

            /// <summary>
            /// RSS version 2.0
            /// </summary>
            /// <param name="item"></param>
            /// <returns></returns>
            private static RssSchema ParseRssItem(XElement item)
            {
                var rssItem = ParseItem(item);

                rssItem.PublishDate = item.GetSafeElementDate("pubDate");

                string image = item.GetSafeElementString("image");
                if (string.IsNullOrEmpty(image))
                {
                    image = item.GetImageFromEnclosure();
                }
                if (string.IsNullOrEmpty(image))
                {
                    image = item.GetImage();
                }
                if (string.IsNullOrEmpty(image) && item.ToString().Contains("media:thumbnail"))
                {
                    XNamespace ns = "http://search.yahoo.com/mrss/";
                    var element = item.Elements(ns + "thumbnail").Last();
                    image = element.Attribute("url").Value;
                }
                if (string.IsNullOrEmpty(image) && item.ToString().Contains("thumbnail"))
                {
                    image = item.GetSafeElementString("thumbnail");
                }

                rssItem.ImageUrl = image;

                return rssItem;
            }
        }
    }

    Kinldy comment the corrected format for me rss, so i just replace the whole element.. thanks and grateful


    better days ahead...........G

    Wednesday, February 26, 2014 12:57 PM
  • Just go to this code under this method..

    rssItem.Summary = Utility.DecodeHtml(description).Trim().Truncate(500, true);

    Just increase the value to 1000 or more within the .Truncate method to an extend and check...

    Wednesday, February 26, 2014 1:09 PM
  • thanks alot for your efforts, but i cant still get this rss to show details on each page unlike others, pls you may try the rss link if you can get it to work..

    http://www.thisdaylive.com/go/search/?search=Business&contenttype=article&sort=date&output=rss

    thanks


    better days ahead...........G

    Wednesday, February 26, 2014 1:30 PM
  • But in the rss you gave, there's only the link to the news and the pub date na?
    Wednesday, February 26, 2014 1:34 PM
  • are u saying that there something wrong with the rss?  each link leads to the website link..

    better days ahead...........G

    Wednesday, February 26, 2014 1:40 PM
  • The feed should look something like this....so that you can pull out info..

    http://dmadmin.dailymirror.lk/index.php?option=com_ninjarsssyndicator&feed_id=16&format=raw

    Wednesday, February 26, 2014 1:46 PM
  • ok thanks anyway... i will just do something else...

    better days ahead...........G

    Wednesday, February 26, 2014 1:48 PM
  • I tried increasing the 
    rssItem.Summary = Utility.DecodeHtml(description).Trim().Truncate(500, true);
    to 
    rssItem.Summary = Utility.DecodeHtml(description).Trim().Truncate(5000, true);
    and it still doesn't work
    Wednesday, February 26, 2014 5:03 PM
  • yea thanks for trying....

    better days ahead...........G

    Wednesday, February 26, 2014 7:39 PM
  • yup. Im giving up on this for now
    Thursday, February 27, 2014 6:16 PM
  • I found the solution here: http://drew5.net/2014/03/12/windows-phone-app-studio-part-2-rss-reader-fix/
    • Proposed as answer by Hakeem Green Tuesday, March 18, 2014 7:59 PM
    Tuesday, March 18, 2014 5:48 AM