Exemple #1
0
        public static async Task <SyndicationFeed> LoadAsync(XmlReader reader, Rss20FeedFormatter Rssformatter, Atom10FeedFormatter Atomformatter, CancellationToken ct)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            XmlReaderWrapper wrappedReader = XmlReaderWrapper.CreateFromReader(reader);

            Atom10FeedFormatter atomSerializer = Atomformatter;

            if (atomSerializer.CanRead(wrappedReader))
            {
                await atomSerializer.ReadFromAsync(wrappedReader, new CancellationToken());

                return(atomSerializer.Feed);
            }
            Rss20FeedFormatter rssSerializer = Rssformatter;

            if (rssSerializer.CanRead(wrappedReader))
            {
                await rssSerializer.ReadFromAsync(wrappedReader, new CancellationToken());

                return(rssSerializer.Feed);
            }
            throw new XmlException(string.Format(SR.UnknownFeedXml, wrappedReader.LocalName, wrappedReader.NamespaceURI));
        }
Exemple #2
0
        public static IList<Feed> GetFeeds()
        {
            var urls = new[]
                {
                    "http://www.simplyvinay.com/GetBlogRss.aspx", "http://www.bbc.co.uk/blogs/formula1/rss.xml",
                    "http://feeds.feedburner.com/AyendeRahien"
                };
            var list = new List<Feed>();

            foreach (var url in urls)
            {
                using (var reader = new CustomXmlReader(url))
                {
                    if (reader.ReadState == ReadState.Initial)
                        reader.MoveToContent();

                    var atom = new Atom10FeedFormatter();
                    if (atom.CanRead(reader))
                    {
                        atom.ReadFrom(reader);
                        list.AddRange(GenerateFeed(atom.Feed, true));
                    }

                    var rss = new Rss20FeedFormatter();
                    if (rss.CanRead(reader))
                    {
                        rss.ReadFrom(reader);
                        list.AddRange(GenerateFeed(rss.Feed, false));
                    }
                }
            }
            return list;
        }
        /// <summary>
        /// Gets an RSS feed from a given URL and returns the resulting XML as a string.
        /// </summary>
        /// <param name="url">URL of the feed to load.</param>
        /// <param name="count">The count of postings to return.</param>
        /// <returns>The feed XML as a string.</returns>
        public SyndicationFeed GetFeed(string url, int count)
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreWhitespace = true;
            settings.CheckCharacters = true;
            settings.CloseInput = true;
            settings.IgnoreComments = true;
            settings.IgnoreProcessingInstructions = true;
            settings.ProhibitDtd = false;

                using (XmlReader reader = XmlReader.Create(url, settings))
                {
                    SyndicationFeedFormatter formatter = null;
                    Atom10FeedFormatter atom = new Atom10FeedFormatter();
                    Rss20FeedFormatter rss = new Rss20FeedFormatter();
                    SyndicationFeed feed;

                    // Determine the format of the feed
                    if (reader.ReadState == ReadState.Initial)
                    {
                        reader.MoveToContent();
                    }

                    if (atom.CanRead(reader))
                    {
                        formatter = atom;
                    }

                    if (rss.CanRead(reader))
                    {
                        formatter = rss;
                    }

                    if (formatter == null)
                    {
                        return null;
                    }

                    formatter.ReadFrom(reader);
                    feed = formatter.Feed;

                    // Remove unwanted items
                    List<SyndicationItem> items = new List<SyndicationItem>();

                    int added = 0;

                    foreach (SyndicationItem i in feed.Items)
                    {
                        items.Add(i);

                        if (added++ == count - 1) break;
                    }

                    feed.Items = items;
                    return feed;
                }
        }
Exemple #4
0
        public static async Task <TSyndicationFeed> LoadAsync <TSyndicationFeed>(XmlReader reader, CancellationToken ct) where TSyndicationFeed : SyndicationFeed, new()
        {
            Atom10FeedFormatter <TSyndicationFeed> atomSerializer = new Atom10FeedFormatter <TSyndicationFeed>();

            if (atomSerializer.CanRead(reader))
            {
                await atomSerializer.ReadFromAsync(reader, ct).ConfigureAwait(false);

                return(atomSerializer.Feed as TSyndicationFeed);
            }

            Rss20FeedFormatter <TSyndicationFeed> rssSerializer = new Rss20FeedFormatter <TSyndicationFeed>();

            if (rssSerializer.CanRead(reader))
            {
                await rssSerializer.ReadFromAsync(reader, ct).ConfigureAwait(false);

                return(rssSerializer.Feed as TSyndicationFeed);
            }

            throw new XmlException(SR.Format(SR.UnknownFeedXml, reader.LocalName, reader.NamespaceURI));
        }
        public static TSyndicationFeed Load <TSyndicationFeed>(XmlReader reader) where TSyndicationFeed : SyndicationFeed, new()
        {
            if (reader == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
            }
            Atom10FeedFormatter <TSyndicationFeed> formatter = new Atom10FeedFormatter <TSyndicationFeed>();

            if (formatter.CanRead(reader))
            {
                formatter.ReadFrom(reader);
                return(formatter.Feed as TSyndicationFeed);
            }
            Rss20FeedFormatter <TSyndicationFeed> formatter2 = new Rss20FeedFormatter <TSyndicationFeed>();

            if (!formatter2.CanRead(reader))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(System.ServiceModel.SR.GetString("UnknownFeedXml", new object[] { reader.LocalName, reader.NamespaceURI })));
            }
            formatter2.ReadFrom(reader);
            return(formatter2.Feed as TSyndicationFeed);
        }
        public static TSyndicationFeed Load <TSyndicationFeed>(XmlReader reader)
            where TSyndicationFeed : SyndicationFeed, new()
        {
            if (reader == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
            }
            Atom10FeedFormatter <TSyndicationFeed> atomSerializer = new Atom10FeedFormatter <TSyndicationFeed>();

            if (atomSerializer.CanRead(reader))
            {
                atomSerializer.ReadFrom(reader);
                return(atomSerializer.Feed as TSyndicationFeed);
            }
            Rss20FeedFormatter <TSyndicationFeed> rssSerializer = new Rss20FeedFormatter <TSyndicationFeed>();

            if (rssSerializer.CanRead(reader))
            {
                rssSerializer.ReadFrom(reader);
                return(rssSerializer.Feed as TSyndicationFeed);
            }
            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnknownFeedXml, reader.LocalName, reader.NamespaceURI)));
        }
		public void CanRead ()
		{
			Rss20FeedFormatter f = new Rss20FeedFormatter ();
			Assert.IsTrue (f.CanRead (CreateReader ("<rss>")), "#1");
			Assert.IsFalse (f.CanRead (CreateReader ("<item>")), "#2");
			Assert.IsFalse (f.CanRead (CreateReader ("<rss xmlns='urn:foo'>")), "#3");
			Assert.IsFalse (f.CanRead (CreateReader ("<feed xmlns='http://www.w3.org/2005/Atom'>")), "#4");
			Assert.IsFalse (f.CanRead (CreateReader ("<hoge>")), "#5");
			XmlReader r = CreateReader ("<rss></rss>");
			r.Read (); // element
			r.Read (); // endelement
			Assert.IsFalse (f.CanRead (r), "#6");

			r = CreateReader ("<rss><channel><title>test</title></channel></rss>");
			r.Read (); // feed
			r.Read (); // channel
			Assert.IsFalse (f.CanRead (r), "#7");
		}
Exemple #8
0
        /// <summary>
        /// Returns a <c>SyndicationFeed</c> given a valid feed url; 
        /// otherwhise it returns null.
        /// </summary>
        /// <param name="feedUrl">
        /// A string of the feed url.
        /// </param>
        /// <returns>
        /// A <c>SyndicationFeed</c> that may be null.
        /// </returns>
        private static SyndicationFeed GetRssData(string feedUrl)
        {
            SyndicationFeed feed = null;

            try
            {
                if (!string.IsNullOrEmpty(feedUrl))
                {
                    XmlReaderSettings settings = new XmlReaderSettings();
                    settings.IgnoreWhitespace = true;
                    settings.CheckCharacters = true;
                    settings.CloseInput = true;
                    settings.IgnoreComments = true;
                    settings.IgnoreProcessingInstructions = true;

                    using (XmlReader reader = XmlReader.Create(feedUrl, settings))
                    {
                        SyndicationFeedFormatter GenericFeedFormatter = null;
                        Atom10FeedFormatter atom = new Atom10FeedFormatter();
                        Rss20FeedFormatter rss = new Rss20FeedFormatter();

                        if (reader.ReadState == ReadState.Initial)
                        {
                            reader.MoveToContent();
                        }

                        // Try either atom or rss reading.
                        if (atom.CanRead(reader))
                        {
                            GenericFeedFormatter = atom;
                        }
                        else if (rss.CanRead(reader))
                        {
                            GenericFeedFormatter = rss;
                        }
                        if (GenericFeedFormatter == null)
                        {
                            return null;
                        }
                        GenericFeedFormatter.ReadFrom(reader);
                        feed = GenericFeedFormatter.Feed;
                    }
                }
            }
            catch (XmlException)
            {
                // Do nothing, but don't shut down
                // This will also make the function return null
            }

            return feed;
        }
        private SyndicationFeed GetFeed(string url)
        {
            using (XmlReader reader = XmlReader.Create(url))
            {
                Rss20FeedFormatter rss = new Rss20FeedFormatter();
                if (rss.CanRead(reader))
                {
                    rss.ReadFrom(reader);
                    return rss.Feed;
                }

                Atom10FeedFormatter atom = new Atom10FeedFormatter();
                if (atom.CanRead(reader))
                {
                    atom.ReadFrom(reader);
                    return atom.Feed;
                }
            }

            return null;
        }
        private void Read(Stream stream, Channel c, RssViewModel m)
        {
            var feedFormatter = new Atom10FeedFormatter();
            var rssFormater = new Rss20FeedFormatter();

            XmlReader atomReader = XmlReader.Create(stream);
            SyndicationFeedFormatter f = null;

            if (feedFormatter.CanRead(atomReader))
            {
                feedFormatter.ReadFrom(atomReader);
                atomReader.Close();
                f = feedFormatter;
            }
            else
                if (rssFormater.CanRead(atomReader))
                {
                    rssFormater.ReadFrom(atomReader);
                    atomReader.Close();
                    f = rssFormater;
                }

            if (f == null) return;

            SyndicationFeed feed = f.Feed;
            c.Title = feed.Title.Text;

            Article[] articles = feed.Items.Select(item => new Article
                {
                    PubDate = item.PublishDate.DateTime,
                    Description = item.Summary.Text,
                    Link = item.Links[0].Uri.OriginalString,
                    Title = item.Title.Text,
                }).Select(art => m.AddArticle(art, c)).ToArray();
            m.DeleteArticle(c.Articles.Where(x => !x.IsStared).Except(articles).ToArray(), c);
            c.LastUpdate = DateTime.Now;
        }