LINQ与ATOM供稿 [英] LINQ with ATOM feeds

查看:172
本文介绍了LINQ与ATOM供稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的Silverlight应用程序调用ATOM源,并显示文章标题和提交日期。我发现这很容易与RSS做饲料和LINQ,但我坚持努力做同样的ATOM源。下面的代码产生任何错误,但它也产生没有结果! ?我想什么



来源ATOM供稿:weblogs.asp.net/scottgu/atom.aspx



来源教程:www.switchonthecode.com/tutorials/silverlight-datagrid-the-basics



源代码:

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;使用System.Net
;使用System.Windows
;使用System.Windows.Controls的
;使用System.Windows.Documents
;
使用System.Windows.Input;使用System.Windows.Media
;
使用System.Windows.Media.Animation;使用System.Windows.Shapes
;
使用System.Xml.Linq的;

命名空间BasicDataGridTutorial
{
公共部分类页:用户控件
{
公共页面()
{
的InitializeComponent() ;
}

私人无效btnPopulate_Click(对象发件人,RoutedEventArgs E)
{
//所以它不是点击禁用填充按钮两次
//而该数据被请求
this.btnPopulate.IsEnabled =假;

//创建一个新的WebClient对象
WebClient的客户端=新的WebClient();

//勾当数据被接收
client.DownloadStringCompleted + = client_DownloadStringCompleted时调用的事件;

//告诉Web客户端下载数据异步
client.DownloadStringAsync(
//新的URI(http://feeds.feedburner.com/SwitchOnTheCode?format= XML));
新的URI(http://weblogs.asp.net/scottgu/atom.aspx));
}

私人无效client_DownloadStringCompleted(对象发件人,
DownloadStringCompletedEventArgs E)
{
this.btnPopulate.IsEnabled = TRUE;
如果(e.Error == NULL)
{
的XDocument文档= XDocument.Parse(e.Result);
的XNamespace的xmlns =htt​​p://www.w3.org/2005/Atom;

变种sotcPosts从document.Descendants进入=(的xmlns +项)
选择新SOTCPost
{
标题=(字符串)entry.Element号(xmlns + feedEntryContent)值,
日期=(字符串)entry.Element号(xmlns +LASTUPDATED)值
}。

this.sotcDataGrid.ItemsSource = sotcPosts;
}
}

私人无效btnClear_Click(对象发件人,RoutedEventArgs E)
{
this.sotcDataGrid.ItemsSource = NULL;
}
}

公共类SOTCPost
{
公共字符串名称{搞定;组; }
公共字符串日期{搞定;组; }
}
}


解决方案

我建议使用 SyndicationFeed 而不是解析Atom提要自己。它会做处理,你可能没有考虑边缘的情况下更好的工作。

 的XmlReader读卡器= XmlReader.Create(HTTP ://localhost/feeds/serializedFeed.xml); 
SyndicationFeed饲料= SyndicationFeed.Load(读卡器);
变种sotcPosts从项目=以feed.Items
选择新SOTCPost
{
标题= item.Title.Text,
日期= item.PublishDate
};


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

解决方案

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.

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

这篇关于LINQ与ATOM供稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆