简体   繁体   English

用c#在wp7中读取rss feed

[英]reading rss feed in wp7 with c#

How to read rss feed from here in wp7 app using c#: " http://www.nyc.gov/apps/311/311Today.rss "? 如何使用c#:“ http://www.nyc.gov/apps/311/311Today.rss ”在wp7应用程序中从此处读取rss feed?

My Xaml code: 我的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>

My C# code: 我的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;

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

A while ago I wrote an entire tutorial covering this topic. 不久前,我写了一个完整的教程,涵盖了这个主题。 Details here: 详细信息在这里:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM