带有 ATOM 提要的 LINQ
我正在尝试创建一个简单的 Silverlight 应用程序,该应用程序调用 ATOM 提要并显示文章标题和提交日期。 我发现使用 RSS 提要和 LINQ 很容易做到这一点,但我却试图用 ATOM 提要做同样的事情。 下面的代码没有产生错误,但也没有产生任何结果! 我缺少什么?
源 ATOM feed:weblogs.asp.net/scottgu/atom.aspx
源教程:www.switchonthecode.com/tutorials/silverlight-datagrid-the-basics
源代码:
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's 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's 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; }
}
}
I am trying to create a simple Silverlight application that calls an ATOM feed and displays the article title and submit date. I found this very easy to do with RSS feeds and LINQ but I am stuck trying to do the same with an ATOM feed. The code below produces no errors but it also produced no results! What am I missing?
Source ATOM feed: weblogs.asp.net/scottgu/atom.aspx
Source Tutorial: www.switchonthecode.com/tutorials/silverlight-datagrid-the-basics
Source code:
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's 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's 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; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用 SyndicateFeed 而不是自己解析 ATOM Feed。 它将更好地处理您可能没有考虑到的边缘情况。
I'd recommend using the SyndicationFeed instead of parsing the ATOM feed yourself. It'll do a better job of handling edge cases you may not have considered.
您有“feedEntryContent”和“lastUpdated”作为元素名称,但我认为您想要“title”和“published”。
您得到“无结果”的原因是文档中不存在您选择的名称的元素。
You have "feedEntryContent" and "lastUpdated" as element names, but I think you want "title" and "published".
The reason you get "no results" is that elements by the names you're selecting don't exist in the document.