Praneeth Puligundla
Praneeth Puligundla

Reputation: 415

The string was not recognized as a valid DateTime. There is an unknown word starting at index 26

I am working on XML feed from RSS, i thinking of sorting the feed by their published date and time. Since different RSS feeds are being taken, they have different data and time formats.

for example

Sat, 23 Mar 2013 23:19:54 GMT
Sat, 23 Mar 2013 23:19:54 EDT
Sat, 23 Mar 2013 23:19:54 -0400

I want to convert them in local time and then sort them.

  var rssFeed = from el in doc.Elements("rss").Elements("channel").Elements("item")
  select new
  {
       Title = el.Element("title").Value,
       Link = el.Element("link").Value,
       Description = el.Element("description").Value,
       PubDate = DateTime.Parse(el.Element("pubDate").Value, null,
                        DateTimeStyles.None)            
  };

Please let me know how to proceed.

Upvotes: 1

Views: 4070

Answers (1)

Manish Mishra
Manish Mishra

Reputation: 12375

may be your DateTime string is not in correct format to be parsed.

use this instead:

DateTime.ParseExact(el.Element("pubDate").Value,
                                  "ddd, dd MM yyyy HH:mm:ss",null);

and pass your appropriate format to get your date.

you will have to get rid of that TimeZone portion in the end.

read more about this here and here

Upvotes: 2

Related Questions