LINQ with ATOM feeds

I am trying to create a simple Silverlight application that calls the ATOM feed and displays the article title and submission date. I found it very easy to do with RSS feeds and LINQ, but I was stuck trying to do the same with the ATOM feed. There are no errors in the code below, but it also did not give any results! What am I missing?

ATOM source: weblogs.asp.net/scottgu/atom.aspx

Source Tutorial: www.switchonthecode.com/tutorials/silverlight-datagrid-the-basics

Source:

using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Xml.Linq; namespace BasicDataGridTutorial { public partial class Page : UserControl { public Page() { InitializeComponent(); } private void btnPopulate_Click(object sender, RoutedEventArgs e) { //disable the populate button so it not clicked twice //while the data is being requested this.btnPopulate.IsEnabled = false; //make a new WebClient object WebClient client = new WebClient(); //hook the event that called when the data is received client.DownloadStringCompleted += client_DownloadStringCompleted; //tell the WebClient to download the data asynchronously client.DownloadStringAsync( //new Uri("http://feeds.feedburner.com/SwitchOnTheCode?format=xml")); new Uri("http://weblogs.asp.net/scottgu/atom.aspx")); } private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { this.btnPopulate.IsEnabled = true; if (e.Error == null) { XDocument document = XDocument.Parse(e.Result); XNamespace xmlns = "http://www.w3.org/2005/Atom"; var sotcPosts = from entry in document.Descendants(xmlns+ "entry") select new SOTCPost { Title = (string)entry.Element(xmlns + "feedEntryContent").Value, Date = (string)entry.Element(xmlns + "lastUpdated").Value }; this.sotcDataGrid.ItemsSource = sotcPosts; } } private void btnClear_Click(object sender, RoutedEventArgs e) { this.sotcDataGrid.ItemsSource = null; } } public class SOTCPost { public string Title { get; set; } public string Date { get; set; } } } 
+4
source share
2 answers

I recommend using SyndicationFeed instead of disassembling the ATOM feed yourself. This is better at handling extreme cases that you might not have considered.

 XmlReader reader = XmlReader.Create("http://localhost/feeds/serializedFeed.xml"); SyndicationFeed feed = SyndicationFeed.Load(reader); var sotcPosts = from item in feed.Items select new SOTCPost { Title = item.Title.Text, Date = item.PublishDate }; 
+11
source

You have "feedEntryContent" and "lastUpdated" as the element names, but I think you want the "title" and the "published".

The reason you get "no results" is because the elements by the names you select do not exist in the document.

+1
source

All Articles