Mackintoast
Mackintoast

Reputation: 1437

Reading atom feeds title and posted datetime

I guess this isn't new at all but I couldn't find a trustworthy link to help me read atom feeds correctly. I would like only to get the feed's title, posted date and time. For example, in the following link http://blogs.technet.com/b/markrussinovich/atom.aspx I would like display

Title 1: The Case of My Mom’s Broken Microsoft Security Essentials Installation   
Date time : 1-5-2005 12:00    
Title 2:.....

Thank you

Upvotes: 0

Views: 3460

Answers (3)

Lloyd
Lloyd

Reputation: 2942

The .Net framework exposes a set of Classes and API's, specifically for working with Syndicated XML Feeds, including RSS 2.0 and Atom 1.0, they can be found in the System.ServiceModel.Syndication namespace.

The basic classes are:
System.ServiceModel.Syndication.SyndicationFeed represents an Atom or RSS formatted XML Feed.
System.ServiceModel.Syndication.SyndicationItem represents the Items within the Feed, "entry" or "Item" elements, these are exposed as a property of the SyndicationFeed IEnumerable Items.

Personally I prefer to use the classes and API's exposed in the System.ServiceModel.Syndication namespace rather than Linq to XML as you are working directly with strongly types objects not ambiguous XElements.

            WebRequest request = WebRequest.Create(this.Url);
            request.Timeout = Timeout;

            using (WebResponse response = request.GetResponse())
            using (XmlReader reader = XmlReader.Create(response.GetResponseStream()))
            {
                SyndicationFeed feed = SyndicationFeed.Load(reader);

                if (feed != null)
                {
                    foreach (var item in feed.Items)
                    {
                         // Work with the Title and PubDate properties of the FeedItem
                    }
                }
            }

Upvotes: 8

Chamika Sandamal
Chamika Sandamal

Reputation: 24312

try this Linq to xml query,

XDocument xml = XDocument.Load("http://blogs.technet.com/b/markrussinovich/atom.aspx");
XNamespace ns = XNamespace.Get("http://www.w3.org/2005/Atom");
var xmlFeed = from post in xml.Descendants(ns + "entry")
              select new
              {
                   Title = post.Element(ns + "title").Value,
                   Time = DateTime.Parse(post.Element(ns + "published").Value)
              };

Upvotes: 1

L.B
L.B

Reputation: 116148

var xdoc = XDocument.Load("http://blogs.technet.com/b/markrussinovich/atom.aspx");
XNamespace ns = XNamespace.Get("http://www.w3.org/2005/Atom");

var info = xdoc.Root
            .Descendants(ns+"entry")
            .Select(n =>
                new
                {
                    Title = n.Element(ns+"title").Value,
                    Time = DateTime.Parse(n.Element(ns+"published").Value),
                }).ToList();

Upvotes: 3

Related Questions