开发者

Serializing data from a RSS feed in ASP.NET

开发者 https://www.devze.com 2022-12-31 17:28 出处:网络
I\'d like the user to specify a RSS feed address and serialize the information from it. I am not interested in the XML format, but populate a strongly typed object from the XML. My question is, is the

I'd like the user to specify a RSS feed address and serialize the information from it. I am not interested in the XML format, but populate a strongly typed object from the XML. My question is, is there a standard that all RSS feeds support (Do all of them have date, title etc)? If so, is there a XSD that describes this. If not, how do I handle serializing a RSS feed to an object in ASP.NET?

EDIT: The SyndicationFeed's Items have the following properties:

  • Title.Text -> Gives us the title
  • Summary.Text -> Gives the Summary

Q1 - The Summary 开发者_JAVA技巧includes the html tags. Is there a way to strip them? I am interested only in the text Q2 - Do all RSS feeds have full content in the Summary element? I see that some RSS feeds have only a few lines for Summary while others have the entire content of the post. Thanks


if you reference the System.ServiceModel.Web there are some options to fetch a feed into a strongly typed object

using (var reader = XmlReader.Create(@"http://newsrss.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml"))
{
    var feed = SyndicationFeed.Load(reader);
    if (feed != null)
    {
        foreach (var item in feed.Items)
        {
            Console.WriteLine(item.Title.Text);
        }
    }
}


Yes RSS is a standard format.

If you search for "C# RSS Reader" you'll find lots of implementations of helper objects that get you the information from the feed.

You could instead just use Linq to XML to get at the information directly from the XML. Scott Guthrie shows you how in his blog his blog.


.NET Framework version 3.5 provides classes to read feeds. This article describes how to do it.

If you are not using 3.5, then you can try Atom.NET (Note: last updated 6 years ago).


The standard at the moment, W3C, is RSS 2.0. RSS 0.91, 0.92 and 2.0 respectively.

Using .Net RSS.Net has always come in handy.


I can suggest you to take a look to Argotic framework. Really easy and usefull, bath for consume and produce RSS feeds.


You can use the following function:

    public object getRSS(string url)
{
    XDocument feedXML = XDocument.Load(url);
    var feeds = from feed in feedXML.Descendants("item")
                select new
                {
                    Title = feed.Element("title").Value,
                    Link = feed.Element("link").Value,
                    Description = feed.Element("description").Value,
                };

    return feeds;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消