c# - 带有 ATOM 源的 LINQ

标签 c# linq silverlight atom-feed

我正在尝试创建一个简单的 Silverlight 应用程序,它调用 ATOM 提要并显示文章标题和提交日期。我发现使用 RSS 提要和 LINQ 很容易做到这一点,但我一直在尝试对 ATOM 提要做同样的事情。下面的代码没有产生任何错误,但也没有产生任何结果!我错过了什么?

来源 ATOM 提要: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; }
  }
}

最佳答案

我建议使用 SyndicationFeed而不是自己解析 ATOM 提要。它会更好地处理您可能没有考虑过的边缘情况。

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
    };

关于c# - 带有 ATOM 源的 LINQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1290851/

相关文章:

c# - 如何在选定的索引更改属性上找到 gridview 的数据键值?

c# - 如果任何参数为空,为什么 Enumerable.SequenceEqual 会抛出异常?

c# - 忽略 Linq.Any 中的大小写,C#

wpf - 从图像中获取路径几何

c# - Silverlight HTTP POST 几个变量,最简单的示例(最少代码)

silverlight - 如何使用代码隐藏变量作为 ValueConverter 中 ConverterParameter 的输入

c# - 使用ASIFormDataRequest时如何实现C#服务器端?

c# - 为什么 Visual Studio 设计器不希望我更改代码中的事件名称?

c# - 比较 C# 中的两个字典

c# - 是否有可能在 .Net C# 中或使用任何 REST API 获取逻辑应用程序工作流操作名称?