您的位置:首页 > 其它

例子:RSS Reader Sample

2013-08-15 15:50 169 查看
本例演示了Rss xml信息的获取,以及如何使用SyndicationFeed来进行符合Rss规范的xml进行解析。

SyndicationFeed 解析完成后 可以得到SyndicationItem数组,她具有以下属性:

名称说明

AttributeExtensions获取联合项的属性扩展。

Authors获取联合项的作者。

BaseUri获取和设置 SyndicationItem 实例的基统一资源标识符 (URI)。

Categories获取联合项的联合类别。

Content获取和设置联合项的内容。

Contributors获取联合项的参与者。

Copyright获取和设置联合项的版权信息。

ElementExtensions获取联合项中包含的元素扩展。

Id获取和设置联合项的 ID。

LastUpdatedTime获取和设置联合项的上次更新时间。

Links获取联合项中包含的链接。

PublishDate获取和设置联合项的发布日期。

SourceFeed获取和设置联合项的源。

Summary获取和设置联合项的摘要。

Title获取和设置联合项的标题。
代码实现:

1. UI中绑定SyndicationFeed相关属性

<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel VerticalAlignment="Top">
<TextBlock TextDecorations="Underline" FontSize="24" Name="feedTitle" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="{StaticResource PhoneAccentBrush}" Text="{Binding Title.Text, Converter={StaticResource RssTextTrimmer}}" />
<TextBlock Name="feedSummary" TextWrapping="Wrap" Margin="12,0,0,0" Text="{Binding Summary.Text, Converter={StaticResource RssTextTrimmer}}" />
<TextBlock Name="feedPubDate" Foreground="{StaticResource PhoneSubtleBrush}" Margin="12,0,0,10" Text="{Binding PublishDate.DateTime}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>


2. 建立网络连接,获取XML数据

// WebClient is used instead of HttpWebRequest in this code sample because
// the implementation is simpler and easier to use, and we do not need to use
// advanced functionality that HttpWebRequest provides, such as the ability to send headers.
WebClient webClient = new WebClient();

// Subscribe to the DownloadStringCompleted event prior to downloading the RSS feed.
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);

// Download the RSS feed. DownloadStringAsync was used instead of OpenStreamAsync because we do not need
// to leave a stream open, and we will not need to worry about closing the channel.
webClient.DownloadStringAsync(new System.Uri("http://n.rss.qq.com/rss/tech_rss.php"));


3. 解析XML,并更新UI

// This method sets up the feed and binds it to our ListBox.
private void UpdateFeedList(string feedXML)
{
// Load the feed into a SyndicationFeed instance
StringReader stringReader = new StringReader(feedXML);
XmlReader xmlReader = XmlReader.Create(stringReader);
SyndicationFeed feed = SyndicationFeed.Load(xmlReader);

// In Windows Phone OS 7.1, WebClient events are raised on the same type of thread they were called upon.
// For example, if WebClient was run on a background thread, the event would be raised on the background thread.
// While WebClient can raise an event on the UI thread if called from the UI thread, a best practice is to always
// use the Dispatcher to update the UI. This keeps the UI thread free from heavy processing.
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// Bind the list of SyndicationItems to our ListBox
feedListBox.ItemsSource = feed.Items;  //SyndicationItem
loadFeedButton.Content = "Refresh Feed";
});

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: