Get SyndicationFeed LastUpdatedTime upto Miliseconds

59 views Asked by At

I am reading a SyndicationFeed by the following method.

XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = new NetworkCredential("username", "password");

// Create the reader.
XmlReaderSettings settings = new XmlReaderSettings();
settings.XmlResolver = resolver;

XmlReader reader = XmlReader.Create(feedUrl, settings);
SyndicationFeed feed = SyndicationFeed.Load(reader);

However, from the Feed, I need the Last updated time up to milliseconds but the code is returning me only up to the seconds part.

feed.LastUpdatedTime is DateTimeOffset and the value is {06/02/2021 00:06:30 +00:00}

// where as this is the data I get back in Postman
<updated>2021-02-06T00:06:30.652Z</updated>

Any idea how do I get the milliseconds part from the atom feed?

1

There are 1 answers

0
Pankaj On BEST ANSWER

I am answering my own question, basically, we do not get this data from Syndication Feed (That always returns 0 milliseconds, looks like a bug in SyndicationFeed class), so we need to parse the XML directly and extract the data from there. Here is my solution for anyone else looking for a similar issue.

DateTime GetUpdatedDateTime(string feeddURl)
{
    const string CustomUtcDateTimeFormat = "yyyy-MM-dd HH:mm:ss:fff"; // 2021-02-09 01:23:34:651

    using(XmlReader reader = XmlReader.Create(feedUrl))
    while (reader.Read())
    {
       if (reader.Name == "updated")
       {
          try
          {
             var lastUpdatedDateTime = reader.ReadElementContentAsString();
             return DateTime.ParseExact(lastUpdatedDateTime, CustomUtcDateTimeFormat);
          }
          catch(Exception ex)
          {
              return DateTime.MinValue;
          }
       }
    }
    return DateTime.MinValue;
 }