Holds details about any errors that occured during the loading or parsing of the RSS feed.
Inheritance: System.EventArgs
Beispiel #1
0
        /// <summary>
        /// Retrieves an RSS feed using the given Url, parses it and
        /// creates and new <see cref="RssFeed">RssFeed</see> object with the information.
        /// If an error occurs in the XML loading of the document, or parsing of
        /// the RSS feed, the error is trapped and stored inside the RssFeed's
        /// ErrorMessage property.
        /// </summary>
        /// <param name="Url">The url to retrieve the RSS feed from, this can
        /// be in the format of http:// and also file://.. (ftp?)</param>
        /// <returns>An <see cref="RssFeed">RssFeed</see> object with information
        /// retrieved from the feed.</returns>
        public RSSFeed Retrieve(string Url)
        {
            RSSFeed rssFeed = new RSSFeed();
              rssFeed.URL = Url;
            rssFeed.Items = new RSSItemCollection();

              XmlTextReader xmlTextReader = new XmlTextReader(Url);
            XmlDocument xmlDoc= new XmlDocument();

              try
              {
            xmlDoc.Load(xmlTextReader);

            // Fire the load event
            if (this.FeedLoaded != null)
            {
              this.FeedLoaded(this, new EventArgs());
            }

            XmlNode rssXmlNode = null;

            // Loop child nodes till we find the rss one
            for (int i = 0; i < xmlDoc.ChildNodes.Count; i++)
            {
              System.Diagnostics.Debug.Write("Child: " + xmlDoc.ChildNodes[i].Name);
              System.Diagnostics.Debug.WriteLine(" has " + xmlDoc.ChildNodes[i].ChildNodes.Count + " children");

              if (xmlDoc.ChildNodes[i].Name == this.rootNodeName && xmlDoc.ChildNodes[i].ChildNodes.Count > 0)
              {
            rssXmlNode = xmlDoc.ChildNodes[i];

            // Fire the found event
            if (this.RssNodeFound != null)
            {
              this.RssNodeFound(this, new EventArgs());
            }

            break;
              }
            }

            if (rssXmlNode != null)
            {
              XmlNode channelXmlNode = null;

              // Loop through the rss node till we find the channel
              for (int i = 0; i < rssXmlNode.ChildNodes.Count; i++)
              {
            System.Diagnostics.Debug.WriteLine("Rss child: " + rssXmlNode.ChildNodes[i].Name);
            if (rssXmlNode.ChildNodes[i].Name == this.channelNodeName && rssXmlNode.ChildNodes[i].ChildNodes.Count > 0)
            {
              channelXmlNode = rssXmlNode.ChildNodes[i];

              // Fire the found event
              if (this.ChannelNodeFound != null)
              {
                this.ChannelNodeFound(this, new EventArgs());
              }

              break;
            }
              }

              // Found the channel node
              if (channelXmlNode != null)
              {
            // Loop through its children, copying details to the
            // RssFeed struct, and parsing the items
            for (int i = 0; i < channelXmlNode.ChildNodes.Count; i++)
            {
              System.Diagnostics.Debug.WriteLine(channelXmlNode.ChildNodes[i].Name);
              switch (channelXmlNode.ChildNodes[i].Name.ToLower())
              {
                case "title":
                  {
                    rssFeed.Title = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "description":
                  {
                    rssFeed.Description = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "language":
                  {
                    rssFeed.Language = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "copyright":
                  {
                    rssFeed.Copyright = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "webmaster":
                  {
                    rssFeed.Webmaster = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "pubDate":
                  {
                    rssFeed.PubDate = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "lastBuildDate":
                  {
                    rssFeed.LastBuildDate = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "category":
                  {
                    rssFeed.Category = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "generator":
                  {
                    rssFeed.Generator = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "ttl":
                  {
                    rssFeed.Ttl = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "rating":
                  {
                    rssFeed.Rating = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "skipHours":
                  {
                    rssFeed.Skiphours = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "skipDays":
                  {
                    rssFeed.Skipdays = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "managingEditor":
                  {
                    rssFeed.ManagingEditor = channelXmlNode.ChildNodes[i].InnerText;
                    break;
                  }
                case "item":
                  {
                    RSSItem t = this.getRssItem(channelXmlNode.ChildNodes[i]);
                    t.Feed = rssFeed;
                    rssFeed.Items.Add(t);

                    // Fire the found event
                    if (this.ItemAdded != null)
                    {
                      this.ItemAdded(this, new EventArgs());
                    }

                    break;
                  }
              }

            }

            // If rdf mode is set, then the channel node only contains
            // information about the channel, it doesn't hold the item
            // nodes. The item nodes are children of the root node in
            // an RDF document, so we use this instead.
            if (this.RdfMode)
            {
              for (int i = 0; i < rssXmlNode.ChildNodes.Count; i++)
              {
                switch (rssXmlNode.ChildNodes[i].Name)
                {
                  case "item":
                    {
                      RSSItem t = this.getRssItem(rssXmlNode.ChildNodes[i]);
                      t.Feed = rssFeed;
                      rssFeed.Items.Add(t);

                      // Fire the found event
                      if (this.ItemAdded != null)
                      {
                        this.ItemAdded(this, new EventArgs());
                      }

                      break;
                    }
                }
              }
            }
              }
              else
              {
            rssFeed.ErrorMessage = "Unable to find rss <seehannel> node";

            // Fire the error event
            if (this.Error != null)
            {
              RSSReaderErrorEventArgs args = new RSSReaderErrorEventArgs();
              args.Message = rssFeed.ErrorMessage;
              this.Error(this, args);
            }
              }

            }
            else
            {
              rssFeed.ErrorMessage = "Unable to find root <rss> node";

              // Fire the error event
              if (this.Error != null)
              {
            RSSReaderErrorEventArgs args = new RSSReaderErrorEventArgs();
            args.Message = rssFeed.ErrorMessage;
            this.Error(this, args);
              }
            }

              }
              catch (XmlException err)
              {
            //
            rssFeed.ErrorMessage = "Xml error: " + err.Message;

            // Fire the error event
            if (this.Error != null)
            {
              RSSReaderErrorEventArgs args = new RSSReaderErrorEventArgs();
              args.Message = rssFeed.ErrorMessage;
              this.Error(this, args);
            }
            return rssFeed;
              }
              catch (WebException we)
              {
            rssFeed.ErrorMessage = "Web error: " + we.Message;
              }

            return rssFeed;
        }
Beispiel #2
0
        /// <summary>
        /// Retrieves an RSS feed using the given Url, parses it and
        /// creates and new <see cref="RssFeed">RssFeed</see> object with the information.
        /// If an error occurs in the XML loading of the document, or parsing of
        /// the RSS feed, the error is trapped and stored inside the RssFeed's
        /// ErrorMessage property.
        /// </summary>
        /// <param name="Url">The url to retrieve the RSS feed from, this can
        /// be in the format of http:// and also file://.. (ftp?)</param>
        /// <returns>An <see cref="RssFeed">RssFeed</see> object with information
        /// retrieved from the feed.</returns>
        public RSSFeed Retrieve(string Url)
        {
            RSSFeed rssFeed = new RSSFeed();

            rssFeed.URL   = Url;
            rssFeed.Items = new RSSItemCollection();

            XmlTextReader xmlTextReader = new XmlTextReader(Url);
            XmlDocument   xmlDoc        = new XmlDocument();

            try
            {
                xmlDoc.Load(xmlTextReader);

                // Fire the load event
                if (this.FeedLoaded != null)
                {
                    this.FeedLoaded(this, new EventArgs());
                }

                XmlNode rssXmlNode = null;

                // Loop child nodes till we find the rss one
                for (int i = 0; i < xmlDoc.ChildNodes.Count; i++)
                {
                    System.Diagnostics.Debug.Write("Child: " + xmlDoc.ChildNodes[i].Name);
                    System.Diagnostics.Debug.WriteLine(" has " + xmlDoc.ChildNodes[i].ChildNodes.Count + " children");

                    if (xmlDoc.ChildNodes[i].Name == this.rootNodeName && xmlDoc.ChildNodes[i].ChildNodes.Count > 0)
                    {
                        rssXmlNode = xmlDoc.ChildNodes[i];

                        // Fire the found event
                        if (this.RssNodeFound != null)
                        {
                            this.RssNodeFound(this, new EventArgs());
                        }

                        break;
                    }
                }

                if (rssXmlNode != null)
                {
                    XmlNode channelXmlNode = null;

                    // Loop through the rss node till we find the channel
                    for (int i = 0; i < rssXmlNode.ChildNodes.Count; i++)
                    {
                        System.Diagnostics.Debug.WriteLine("Rss child: " + rssXmlNode.ChildNodes[i].Name);
                        if (rssXmlNode.ChildNodes[i].Name == this.channelNodeName && rssXmlNode.ChildNodes[i].ChildNodes.Count > 0)
                        {
                            channelXmlNode = rssXmlNode.ChildNodes[i];

                            // Fire the found event
                            if (this.ChannelNodeFound != null)
                            {
                                this.ChannelNodeFound(this, new EventArgs());
                            }

                            break;
                        }
                    }

                    // Found the channel node
                    if (channelXmlNode != null)
                    {
                        // Loop through its children, copying details to the
                        // RssFeed struct, and parsing the items
                        for (int i = 0; i < channelXmlNode.ChildNodes.Count; i++)
                        {
                            System.Diagnostics.Debug.WriteLine(channelXmlNode.ChildNodes[i].Name);
                            switch (channelXmlNode.ChildNodes[i].Name.ToLower())
                            {
                            case "title":
                            {
                                rssFeed.Title = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "description":
                            {
                                rssFeed.Description = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "language":
                            {
                                rssFeed.Language = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "copyright":
                            {
                                rssFeed.Copyright = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "webmaster":
                            {
                                rssFeed.Webmaster = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "pubDate":
                            {
                                rssFeed.PubDate = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "lastBuildDate":
                            {
                                rssFeed.LastBuildDate = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "category":
                            {
                                rssFeed.Category = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "generator":
                            {
                                rssFeed.Generator = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "ttl":
                            {
                                rssFeed.Ttl = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "rating":
                            {
                                rssFeed.Rating = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "skipHours":
                            {
                                rssFeed.Skiphours = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "skipDays":
                            {
                                rssFeed.Skipdays = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "managingEditor":
                            {
                                rssFeed.ManagingEditor = channelXmlNode.ChildNodes[i].InnerText;
                                break;
                            }

                            case "item":
                            {
                                RSSItem t = this.getRssItem(channelXmlNode.ChildNodes[i]);
                                t.Feed = rssFeed;
                                rssFeed.Items.Add(t);

                                // Fire the found event
                                if (this.ItemAdded != null)
                                {
                                    this.ItemAdded(this, new EventArgs());
                                }

                                break;
                            }
                            }
                        }

                        // If rdf mode is set, then the channel node only contains
                        // information about the channel, it doesn't hold the item
                        // nodes. The item nodes are children of the root node in
                        // an RDF document, so we use this instead.
                        if (this.RdfMode)
                        {
                            for (int i = 0; i < rssXmlNode.ChildNodes.Count; i++)
                            {
                                switch (rssXmlNode.ChildNodes[i].Name)
                                {
                                case "item":
                                {
                                    RSSItem t = this.getRssItem(rssXmlNode.ChildNodes[i]);
                                    t.Feed = rssFeed;
                                    rssFeed.Items.Add(t);

                                    // Fire the found event
                                    if (this.ItemAdded != null)
                                    {
                                        this.ItemAdded(this, new EventArgs());
                                    }

                                    break;
                                }
                                }
                            }
                        }
                    }
                    else
                    {
                        rssFeed.ErrorMessage = "Unable to find rss <seehannel> node";

                        // Fire the error event
                        if (this.Error != null)
                        {
                            RSSReaderErrorEventArgs args = new RSSReaderErrorEventArgs();
                            args.Message = rssFeed.ErrorMessage;
                            this.Error(this, args);
                        }
                    }
                }
                else
                {
                    rssFeed.ErrorMessage = "Unable to find root <rss> node";

                    // Fire the error event
                    if (this.Error != null)
                    {
                        RSSReaderErrorEventArgs args = new RSSReaderErrorEventArgs();
                        args.Message = rssFeed.ErrorMessage;
                        this.Error(this, args);
                    }
                }
            }
            catch (XmlException err)
            {
                //
                rssFeed.ErrorMessage = "Xml error: " + err.Message;

                // Fire the error event
                if (this.Error != null)
                {
                    RSSReaderErrorEventArgs args = new RSSReaderErrorEventArgs();
                    args.Message = rssFeed.ErrorMessage;
                    this.Error(this, args);
                }
                return(rssFeed);
            }
            catch (WebException we)
            {
                rssFeed.ErrorMessage = "Web error: " + we.Message;
            }

            return(rssFeed);
        }