0

如何使用 c# 在 wp7 应用程序中从此处读取 rss 提要:“ http://www.nyc.gov/apps/311/311Today.rss ”?

我的 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;

    }
4

2 回答 2

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();
}
于 2012-07-23T20:14:45.203 回答
0

不久前,我写了一个涵盖这个主题的完整教程。详情在这里:

于 2012-07-23T23:13:43.950 回答