コード例 #1
0
        private bool TryParseViaXDocument(FeedSource feedSource, string downloadResult)
        {
            bool parsedSuccessfully = true;
              List<FeedItem> feedItemsToAdd = new List<FeedItem>();

              try
              {
            XDocument document = XDocument.Parse(downloadResult);
            var results = from c in document.Root.Element("channel").Elements("item")
                      select c;
            foreach (var xElement in results)
            {
              FeedItem newItem = new FeedItem();

              newItem.Summary = xElement.Element("description").Value;
              newItem.Title = xElement.Element("title").Value;
              newItem.PostUri = new Uri(xElement.Element("link").Value);
              newItem.Source = feedSource;

              feedItemsToAdd.Add(newItem);
            }
              }
              catch (Exception e)
              {
            parsedSuccessfully = false;
            if (feedItemsToAdd != null)
              feedItemsToAdd.Clear();
            Services.Log("SyndicationFeed did not work with url " + feedSource.UriString + "\n" +
                     "Exception.Message: " + e.Message,
                     LogPriority.Low,
                     LogCategory.Information);
              }

              if (parsedSuccessfully)
              {
            //copy method-scoped feeditems to higher level scope results object
            foreach (var item in feedItemsToAdd)
            {
              _DownloadFeedResults.FeedItems.Add(item);
            }
              }

              return parsedSuccessfully;
        }
コード例 #2
0
        private bool TryParseViaSyndicationFeed(FeedSource downloadRssSource, string downloadResult)
        {
            bool parsedSuccessfully = true;
              List<FeedItem> FeedItemsToAdd = new List<FeedItem>();

              try
              {
            XmlReader xmlReader = XmlReader.Create(new StringReader(downloadResult));
            SyndicationFeed synFeed = SyndicationFeed.Load(xmlReader);

            foreach (var synFeedItem in synFeed.Items)
            {
              if (synFeedItem != null &&
              synFeedItem.Title != null &&
              synFeedItem.Links.Count > 0)
              {
            FeedItem FeedItem = new FeedItem();

            FeedItem.Title = synFeedItem.Title.Text;
            if (synFeedItem.Summary != null)
              FeedItem.Summary = synFeedItem.Summary.Text;
            //links
            Uri postUri = null;
            foreach (var link in synFeedItem.Links)
            {
              string relType = link.RelationshipType.ToString();
              if (relType == "self" || relType == "replies")
                continue;
              else
                postUri = link.GetAbsoluteUri();
            }
            if (postUri == null)
              postUri = synFeedItem.Links[0].GetAbsoluteUri();
            FeedItem.PostUri = postUri;
            //FeedItem.PostUri = synFeedItem.Links[0].GetAbsoluteUri();
            FeedItem.Source = downloadRssSource;

            FeedItemsToAdd.Add(FeedItem);//add to method-scoped list first
              }
            }
              }
              catch (Exception e)
              {
            parsedSuccessfully = false;
            if (FeedItemsToAdd != null)
              FeedItemsToAdd.Clear();
            Services.Log("SyndicationFeed did not work with url " + downloadRssSource.UriString + "\n" +
                     "Exception.Message: " + e.Message,
                     LogPriority.Low,
                     LogCategory.Information);
              }

              if (parsedSuccessfully)
              {
            //copy method-scoped feeditems to higher level scope results object
            foreach (var item in FeedItemsToAdd)
            {
              _DownloadFeedResults.FeedItems.Add(item);
            }
              }

              return parsedSuccessfully;
        }