Exceptions with DateTime handling in RSS feed in C #

I am trying to parse Rss2 Atom feeds using SyndicationFeedFormatter and SyndicationFeed objects. But I get XmlExceptions when parsing a DateTime field like pubDate and / or lastBuildDate.

Wed, Feb 24, 2010 6:56:04 PM GMT + 00: 00 not working

Wed, Feb 24, 2010 6:56:04 PM GMT

So, he rushed about from the time zone field.

As a workaround for familiar feeds, I manually fixed these DateTime nodes - catching an XmlException, loading Rss into an XmlDocument, setting the value of these nodes, creating a new XmlReader, and then returning the formatter from this new XmlReader object (code not shown). But for this approach to work, I need to know in advance which nodes throw an exception.

SyndicationFeedFormatter syndicationFeedFormatter = null; XmlReaderSettings settings = new XmlReaderSettings(); using (XmlReader reader = XmlReader.Create(url, settings)) { try { syndicationFeedFormatter = SyndicationFormatterFactory.CreateFeedFormatter(reader); syndicationFeedFormatter.ReadFrom(reader); } catch (XmlException xexp) { // fix those datetime nodes with exceptions and read again. } return syndicationFeedFormatter; } 

rss channel: http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&q=test&cf=all&output=rss

exceptions:

XmlException Error at position 1 376. An error occurred while parsing the DateTime value in XML.
in System.ServiceModel.Syndication.Rss20FeedFormatter.DateFromString (String dateTimeString, XmlReader reader)
in System.ServiceModel.Syndication.Rss20FeedFormatter.ReadXml (XmlReader reader, result of SyndicationFeed) in System.ServiceModel.Syndication.Rss20FeedFormatter.ReadFrom (XmlReader reader) in ... cs: line 171

 <rss version="2.0"> <channel> ... <pubDate>Wed, 24 Feb 2010 18:56:04 GMT+00:00</pubDate> <lastBuildDate>Wed, 24 Feb 2010 18:56:04 GMT+00:00</lastBuildDate> <-----exception ... <item> ... <pubDate>Wed, 24 Feb 2010 16:17:50 GMT+00:00</pubDate> <lastBuildDate>Wed, 24 Feb 2010 18:56:04 GMT+00:00</lastBuildDate> </item> ... </channel> </rss> 

Is there a better way to achieve this? Please help. Thank you

+6
datetime exception parsing rss
source share
2 answers

Here is my hacking solution for reading Google News RSS feeds.

 string xml; using (WebClient webClient = new WebClient()) { xml = Encoding.UTF8.GetString(webClient.DownloadData(url)); } xml = xml.Replace("+00:00", ""); byte[] bytes = System.Text.UTF8Encoding.ASCII.GetBytes(xml); XmlReader reader = XmlReader.Create(new MemoryStream(bytes)); SyndicationFeed feed = SyndicationFeed.Load(reader); 
+9
source share

to convert PublishDate to RSS on your datetime computer, you could write these lines

  string dateStr = item.PublishDate.ToString("ddd MMM dd HH:mm:ss zzzz yyyy"); DateTime PostDate = DateTime.ParseExact(dateStr, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture); 
0
source share

All Articles