Amazing-Ren

导航

例子:RSS Reader Sample

本例演示了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";
            });

        }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2013-08-15 15:50  Amazing-Ren  阅读(304)  评论(0编辑  收藏  举报