rss阅读器,Windows Phone 7 网络编程之RSS阅读器

实现一个RSS阅读器,通过你输入的RSS地址来获取RSS的信息列表和查看RSS文章中的详细内容。RSS阅读器是使用了WebClient类来获取网络上的RSS的信息,然后再转化为自己定义好的RSS实体类对象的列表,最后绑定到页面上。
(1) RSS实体类和RSS服务类
RssItem.cs using System.Net; using System.Text.RegularExpressions; namespace WindowsPhone.Helpers { /// /// RSS对象类 /// public class RssItem { /// /// 初始化一个RSS目录 /// /// 标题
/// 内容
/// 发表事件
/// 文章地址
public RssItem(string title, string summary, string publishedDate, string url) { Title = title; Summary = summary; PublishedDate = publishedDate; Url = url; //解析html PlainSummary = HttpUtility.HtmlDecode(Regex.Replace(summary, "<[^>]+?>", "")); } //标题 public string Title { get; set; } //内容 public string Summary { get; set; } //发表时间 public string PublishedDate { get; set; } //文章地址 public string Url { get; set; } //解析的文本内容 public string PlainSummary { get; set; } } }

RssService.cs
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.ServiceModel.Syndication; using System.Xml; namespace WindowsPhone.Helpers { /// /// 获取网络RSS服务类 /// public static class RssService { /// /// 获取RSS目录列表 /// /// RSS的网络地址
/// 获取完成事件
public static void GetRssItems(string rssFeed, Action> _disibledevent=>= null, Action _disibledevent=>= null, Action _disibledevent=>= null) { WebClient webClient = new WebClient(); //注册webClient读取完成事件 webClient.OpenReadCompleted += delegate(object sender, OpenReadCompletedEventArgs e) { try { if (e.Error != null) { if (onError != null) { _disibledevent=>return; } //将网络获取的信息转化成RSS实体类 List rssItems = new List(); Stream stream = e.Result; XmlReader response = XmlReader.Create(stream); SyndicationFeed feeds = SyndicationFeed.Load(response); foreach (SyndicationItem f in feeds.Items) { RssItem rssItem = new RssItem(f.Title.Text, f.Summary.Text, f.PublishDate.ToString(), f.Links[0].Uri.AbsoluteUri); rssItems.Add(rssItem); } //通知完成返回事件执行 if (onGetRssItemsCompleted != null) { _disibledevent=>finally { if (onFinally != null) { _disibledevent=>new Uri(rssFeed)); } } }

(2) RSS页面展示
MainPage.xaml

最新评论

发表评论