用c#读取rss以wp7为单位

时间:2012-07-23 20:05:43

标签: c# windows-phone-7 xaml

如何使用c#:“http://www.nyc.gov/apps/311/311Today.rss”从wp7 app中读取rss feed?

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

    }

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:0)