Take the 2-minute tour ×

So here's my XML file:

    <title>Book Title</title>
    <author>Book Author</author>
        <date format="standard">1991-01-15</date> 
        <date format="friendly">January 1991</date> 

I'm loading the data into an XDocument, then retrieving it from the XDocument and adding it into a Book class, but I'm having trouble getting the date. I'd like to retrieve the friendly date.

Here's what I have:

XDocument xml = XDocument.Load("http://www.mysite.com/file.xml");

books.Add(new Book
                    Title = xml.Root.Element("title").Value,
                    Author = xml.Root.Element("author").Value,
                    //PubDate = 

How can I get the friendly date?

share|improve this question

2 Answers 2

up vote 3 down vote accepted
PubDate = DateTime.ParseExact(xml.Root.Elements("pubDates")
.Where(n => n.Attribute("format").Value == "standard")
, "yyyy-mm-dd", CultureInfo.InvariantCulture);
share|improve this answer
This works great, thank you. –  Steven May 26 '10 at 22:54
I noticed that you asked for the friendly date, but I figured if you get a real DateTime value instead, you can then do anything you want with it. –  Tergiver May 27 '10 at 0:37

I haven't tested this, but it should look something like this:

from node in xml.DescendantNodes("pubDates").DescendantNodes("date")
where node.Attribute("format").Value == "friendly"
select node.Value.FirstOrDefault()
share|improve this answer

Your Answer


