2009-08-17 2 views
4

ATOM 피드를 호출하고 기사 제목과 제출 날짜를 표시하는 간단한 Silverlight 응용 프로그램을 만들려고합니다. RSS 피드와 LINQ를 사용하여이 작업을 수행하기가 매우 쉽지만 ATOM 피드를 사용하여 동일한 작업을 수행하려고합니다. 아래의 코드는 오류는 발생하지 않지만 결과도 나타나지 않습니다! 내가 뭘 놓치고 있니?ATOM 피드가있는 LINQ

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

답변

1

요소 이름으로 "feedEntryContent"및 "lastUpdated"가 있지만 제목과 게시가 필요하다고 생각합니다.

"결과 없음"을 얻는 이유는 선택한 이름의 요소가 문서에 존재하지 않기 때문입니다.

11

직접 ATOM 피드를 구문 분석하는 대신 SyndicationFeed을 사용하는 것이 좋습니다. 그것은 당신이 고려하지 않았을 수있는 가장자리 케이스를 다루는 더 나은 일을 할 것입니다.

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

감사합니다. :) –

관련 문제