例子:RSS Reader Sample
2013-08-15 15:50
169 查看
本例演示了Rss xml信息的获取,以及如何使用SyndicationFeed来进行符合Rss规范的xml进行解析。
SyndicationFeed 解析完成后 可以得到SyndicationItem数组,她具有以下属性:
代码实现:
1. UI中绑定SyndicationFeed相关属性
2. 建立网络连接,获取XML数据
3. 解析XML,并更新UI
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"; }); }
相关文章推荐
- 有关于怎么生成一个RSS文档的小例子
- SqlDataReader对象使用例子
- File Reader sample
- .Net/C#: 利用反射编写通用的 rss 2.0 的 reader
- About RSS Reader
- 用Ajax读取RSS种子的简单例子
- 例子:Basic Lens sample
- 如何导入android中的sample例子到eclipse中
- .Net/C#: 利用反射编写通用的 rss 2.0 的 reader
- Python CSV Reader/Writer 例子
- 例子:Camera Color Picker Sample (YCbCr->ARGB)
- java StringReader使用例子代码
- 将android中的sample例子到eclipse中
- 一个异常:Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class com.rssReader.st
- RSS 阅读工具Omea Reader
- RSS4J解析网络上指定URL的RSS文档的例子
- 备忘:苹果官方例子Guides and Sample Code
- ffmpeg在ubuntu11.10下编译avcodec_sample.c例子
- 灵活组装Json的数据使用Gson的JsonParser和JsonReader解析Json详解例子
- stl sample例子