起初,你需要阅读应该是一个XML文件,我建议你使用XPath和LINQ到XML,并且你已经说主要有三个要素构成饲料; “标题”,“链接”和“描述”。
不是很久以前我写了一个代码来做到这一点,我希望这对你有用。
我创建了这两个实体。
public class RssFeed
{
public string Title { get; set; }
public string Link { get; set; }
public string Description { get; set; }
public string PubDate { get; set; }
public string Language { get; set; }
public ObservableCollection<RssItem> RssItems { get; set; }
}
public class RssItem
{
public string Title { get; set; }
public string Description { get; set; }
public string Link { get; set; }
}
然后在这个方法我用Linq to XML
private static void ReadFeeds()
{
string uri = @"http://news.yahoo.com/rss/entertainment";
WebClient client = new WebClient();
client.DownloadStringAsync(new Uri(uri, UriKind.Absolute));
client.DownloadStringCompleted += (s, a) =>
{
if (a.Error == null && !a.Cancelled)
{
var rssReader = XDocument.Parse(a.Result);
var feed = (from rssFeed in rssReader.Descendants("channel")
select new RssFeed()
{
Title = null != rssFeed.Descendants("title").FirstOrDefault() ?
rssFeed.Descendants("title").First().Value : string.Empty,
Link = null != rssFeed.Descendants("link").FirstOrDefault() ?
rssFeed.Descendants("link").First().Value : string.Empty,
Description = null != rssFeed.Descendants("description").FirstOrDefault() ?
rssFeed.Descendants("description").First().Value : string.Empty,
PubDate = null != rssFeed.Descendants("pubDate").FirstOrDefault() ?
rssFeed.Descendants("pubDate").First().Value : string.Empty,
Language = null != rssFeed.Descendants("language").FirstOrDefault() ?
rssFeed.Descendants("language").First().Value : string.Empty
}).Single();
var rssFeeds = (from rssItems in rssReader.Descendants("item")
select new RssItem()
{
Title = null != rssItems.Descendants("title").FirstOrDefault() ?
rssItems.Descendants("title").First().Value : string.Empty,
Link = null != rssItems.Descendants("link").FirstOrDefault() ?
rssItems.Descendants("link").First().Value : string.Empty,
Description = null != rssItems.Descendants("description").FirstOrDefault() ?
rssItems.Descendants("description").First().Value : string.Empty,
}).ToList();
feed.RssItems = new ObservableCollection<RssItem>(rssFeeds);
}
};
}
读取XML文件中的每一个元素最后,你必须显示你的饲料,无论你想。
为了解析任何数据,你需要知道它的格式。要做到这一点,你需要找到它的规格。在这种情况下,谷歌RSS规范生病得到你所需要的,如http://cyber.law.harvard.edu/rss/rss.html。有一点需要考虑的是你是否在重新发明轮子。很有可能有人已经写了一些东西来解析RSS提要,你可以找到它。 http://stackoverflow.com/questions/576267/c-sharp-rss-reader也可能是一个有用的参考。 – Chris