2010-02-24 9 views
6

Sto cercando di analizzare Rss2, i feed Atom che utilizzano oggetti SyndicationFeedFormatter e SyndicationFeed. Ma ricevo XmlExceptions mentre analizzo il campo DateTime come pubDate e/o lastBuildDate.Eccezioni con l'analisi DateTime nel feed RSS in C#

Wed, 24 Feb 2010 18:56:04 GMT + 00: 00 non funziona

Wed, 24 Feb 2010 18:56:04 GMT funziona

così, è gettando a causa del fuso orario campo.

Come soluzione alternativa, per i familiari feed vorrei correggere manualmente quei nodi DateTime - catturando l'XmlException, il caricamento del Rss in un XmlDocument, fissando il valore quei nodi, creando un nuovo XmlReader e poi tornare il formattatore da questa nuova XmlReader oggetto (codice non mostrato). Ma per questo approccio al lavoro, ho bisogno di sapere in anticipo quali nodi fanno eccezione.

 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 Feed: http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&q=test&cf=all&output=rss

detials di eccezione: Errore

XmlException nella riga 1 posizione 376. è verificato un errore durante l'analisi di un valore DateTime in XML.
a System.ServiceModel.Syndication.Rss20FeedFormatter.DateFromString (String dateTimeString, lettore di XmlReader)
a System.ServiceModel.Syndication.Rss20FeedFormatter.ReadXml (XmlReader lettore, risultato SyndicationFeed) a System.ServiceModel.Syndication. Rss20FeedFormatter.ReadFrom (XmlReader lettore) a ... cs: linea 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> 

esiste un modo migliore per raggiungere questo obiettivo? Per favore aiuto. Grazie.

risposta

9

Ecco la mia soluzione alternativa per leggere i feed RSS di Google News.

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); 
+2

Nizza correzione. Odio che dobbiamo farlo. Anche i miei amici di PHP mi guardano di sbieco quando parlo di un servizio che lancia errori in qualcosa di banale come questo. –

+0

Questa è una buona idea, ma cosa succede se la stringa non è 00:00, se è un altro fuso orario? Penso che sia meglio usare Regex qui invece di Sostituire –

0

per convertire PublishDate RSS al vostro datetime computer che si potrebbe scrivere queste righe

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);