Answers:
添加System.ServiceModel
参考
string url = "http://fooblog.com/feed";
XmlReader reader = XmlReader.Create(url);
SyndicationFeed feed = SyndicationFeed.Load(reader);
reader.Close();
foreach (SyndicationItem item in feed.Items)
{
String subject = item.Title.Text;
String summary = item.Summary.Text;
...
}
System.ServiceModel.Syndication
这是一篇过时的文章,但是如果像现在这样来到这里,可以节省一些人的时间,建议您看一下CodeHollow.FeedReader程序包,该程序包支持更广泛的RSS版本,更易于使用,并且看起来更可靠。https://github.com/codehollow/FeedReader
您正在寻找的SyndicationFeed
类,正是这样做的。
用这个 :
private string GetAlbumRSS(SyndicationItem album)
{
string url = "";
foreach (SyndicationElementExtension ext in album.ElementExtensions)
if (ext.OuterName == "itemRSS") url = ext.GetObject<string>();
return (url);
}
protected void Page_Load(object sender, EventArgs e)
{
string albumRSS;
string url = "http://www.SomeSite.com/rss";
XmlReader r = XmlReader.Create(url);
SyndicationFeed albums = SyndicationFeed.Load(r);
r.Close();
foreach (SyndicationItem album in albums.Items)
{
cell.InnerHtml = cell.InnerHtml +string.Format("<br \'><a href='{0}'>{1}</a>", album.Links[0].Uri, album.Title.Text);
albumRSS = GetAlbumRSS(album);
}
}
更新:仅支持UWP- Windows Community Toolkit
现在有一种简单得多的方法。您可以使用RssParser类。示例代码如下。
public async void ParseRSS()
{
string feed = null;
using (var client = new HttpClient())
{
try
{
feed = await client.GetStringAsync("https://visualstudiomagazine.com/rss-feeds/news.aspx");
}
catch { }
}
if (feed != null)
{
var parser = new RssParser();
var rss = parser.Parse(feed);
foreach (var element in rss)
{
Console.WriteLine($"Title: {element.Title}");
Console.WriteLine($"Summary: {element.Summary}");
}
}
}
对于非UWP,请System.ServiceModel.Syndication
按照其他建议使用来自名称空间的联合。
public static IEnumerable <FeedItem> GetLatestFivePosts() {
var reader = XmlReader.Create("https://sibeeshpassion.com/feed/");
var feed = SyndicationFeed.Load(reader);
reader.Close();
return (from itm in feed.Items select new FeedItem {
Title = itm.Title.Text, Link = itm.Id
}).ToList().Take(5);
}
public class FeedItem {
public string Title {
get;
set;
}
public string Link {
get;
set;
}
}