private bool TryRSSFormat(string url) { if (string.IsNullOrEmpty(url) || string.IsNullOrWhiteSpace(url)) return false; // set the wait cursor // create a new xml doc XmlDocument doc = new XmlDocument(); DateTime date; try { // load the xml doc doc.Load(url); date = DateTime.Now; } catch (Exception ex1) { //// return the cursor //this.Cursor = Cursors.Arrow; // tell a story MessageBox.Show(ex1.Message, "Atmel Studio - TryRSSFormat"); return false; } // get an xpath navigator XPathNavigator navigator = doc.CreateNavigator(); list.Clear(); //Dictionary<string, RSSFeedDataset> list = new Dictionary<string, RSSFeedDataset>(); try { // look for the path to the rss item titles; navigate // through the nodes to get all titles XPathNodeIterator nodes; nodes = navigator.Select("//rss/channel/item/title"); while (nodes.MoveNext()) { // clean up the text for display XPathNavigator node = nodes.Current; if (node != null) { string tmp = node.Value.Trim(); tmp = tmp.Replace("\n", ""); tmp = tmp.Replace("\r", ""); // add a new treeview node for this // news item title var item = new RSSFeedDataset(); item.Subject = tmp; item.Time = date.ToString(); list.Add(item); } } // set a position counter int position = 0; // Get the links from the RSS feed XPathNodeIterator nodesLink = navigator.Select("//rss/channel/item/link"); while (nodesLink.MoveNext()) { // clean up the link XPathNavigator node = nodesLink.Current; if (node != null) { string tmp = node.Value.Trim(); tmp = tmp.Replace("\n", ""); tmp = tmp.Replace("\r", ""); // use the position counter // to add a link child node // to each news item title list[position].Url = tmp; } // increment the position counter position++; } position = 0; // Get the links from the RSS feed XPathNodeIterator nodesDescription = navigator.Select("//rss/channel/item/description"); while (nodesDescription.MoveNext()) { // clean up the link XPathNavigator node = nodesDescription.Current; if (node != null) { string tmp = node.Value.Trim(); //tmp = tmp.Replace("\n", ""); //tmp = tmp.Replace("\r", ""); // use the position counter // to add a link child node // to each news item title list[position].Description = tmp; } // increment the position counter position++; } //foreach (var item in list) //{ // rssfeedcollection.Add(item); //} //if (list.Any()) //{ // Dispatcher.CurrentDispatcher.Invoke(new Action<object>((Action) => // { // foreach (var item in list) // { // rssfeedcollection.Add(item); // } // })); //} } catch (Exception ex) { MessageBox.Show(ex.Message, "RSS Feed Load Error"); return false; } switch (list.Any()) { case false: return false; default: return true; } }
private bool TryAtomFormat(string url) { try { //rssfeedcollection = new ObservableCollection<RSSFeedDataset>(); WebRequest request = WebRequest.Create(url); request.Timeout = 50000; using (WebResponse response = request.GetResponse()) using (XmlReader reader = XmlReader.Create(response.GetResponseStream())) { SyndicationFeed feed = SyndicationFeed.Load(reader); DateTime date = DateTime.Now; if (feed != null) { //List<RSSFeedDataset> list = new List<RSSFeedDataset>(); list.Clear(); foreach (var nodeitem in feed.Items) { // Work with the Title and PubDate properties of the FeedItem var item = new RSSFeedDataset(); item.Subject = nodeitem.Title.Text; item.Time = date.ToString(CultureInfo.InvariantCulture); item.Url = nodeitem.Id; //item.Title.Text //item.Id if (nodeitem.Summary != null) { //item.Summary.Text item.Description = nodeitem.Summary.Text; if (nodeitem.Content != null) { item.Description += ((System.ServiceModel.Syndication.TextSyndicationContent)(nodeitem.Content)).Text; } } else { if (nodeitem.Content != null) { item.Description = ((System.ServiceModel.Syndication.TextSyndicationContent)(nodeitem.Content)).Text; } } list.Add(item); //rssfeedcollection.Add(item); } //if (list.Any()) //{ // //Dispatcher.CurrentDispatcher.Invoke(new Action<object>((Action) => // // { // // foreach (var item in list) // // { // // rssfeedcollection.Add( // // item); // // } // // })); // Dispatcher.CurrentDispatcher.Invoke(new Action<object>((Action) => // { // for(int i=0;i<list.Count;i++) // rssfeedcollection.Add(list[i]); // })); //} } } if (list.Any() == false) return false; } catch (Exception ex) { MessageBox.Show(ex.Message, "AtmelStudio - TryAtomFormat"); return false; } return true; }