2012-07-23 99 views
0

如何閱讀從wp7應用程序使用c#的「rss飼料」:「http://www.nyc.gov/apps/311/311Today.rss」?閱讀rss飼料wp7與c#

我的XAML代碼:

<ListBox HorizontalAlignment="Left" Margin="10,10,0,0" Name="listBox1" VerticalAlignment="Top" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel VerticalAlignment="Top"> 
         <TextBlock x:Name="titleTxt" Height="30" Text="{Binding Title}" VerticalAlignment="Bottom" /> 
         <TextBlock x:Name="dateTxt" Height="30" Text="{Binding Date}" /> 
         <TextBlock x:Name="descTxt" Height="30" Text="{Binding Desc}" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

我的C#代碼:

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 
    { 
     XDocument xDoc = XDocument.Load("http://www.nyc.gov/apps/311/311Today.rss"); 
     XNamespace content = XNamespace.Get("http://purl.org/rss/1.0/modules/content/"); 
     var items = xDoc.Descendants("item") 
       .Select(i => new 
       { 
        Title = i.Element("title").Value, 
        Date = DateTime.Parse(i.Element("pubDate").Value), 
        Desc = i.Element(content + "encoded").Value, 
       }) 
       .ToArray(); 
     listBox1.ItemsSource = items; 

    } 
+1

那麼[?你嘗試過什麼(http://www.whathaveyoutried.com) – 2012-07-23 20:07:33

+0

@ErikPhilips我已經從MSDN嘗試了樣本代碼,但什麼都沒有來。 – Shan 2012-07-23 20:09:27

回答

0
using (var wc = new WebClient()) 
{ 
    XDocument xDoc = XDocument.Parse(wc.DownloadString("http://www.nyc.gov/apps/311/311Today.rss")); 
    XNamespace content = XNamespace.Get("http://purl.org/rss/1.0/modules/content/"); 
    var items = xDoc.Descendants("item") 
      .Select(i => new 
      { 
       Title = i.Element("title").Value, 
       Date = DateTime.Parse(i.Element("pubDate").Value), 
       Desc = i.Element(content + "encoded").Value, 
      }) 
      .ToArray(); 
} 
+0

我應該在哪裏添加上面的代碼? – Shan 2012-07-23 20:26:45

+0

我已在MainPage_loaded事件中添加,但顯示異常。 – Shan 2012-07-23 20:28:52

+0

'顯示異常'?任何細節? – 2012-07-23 20:32:15