Exceptions with DateTime parsing in RSS feed in C#

后端 未结 2 1617
名媛妹妹
名媛妹妹 2021-02-04 19:13

I\'m trying to parse Rss2, Atom feeds using SyndicationFeedFormatter and SyndicationFeed objects. But I\'m getting XmlExceptions while parsing DateTime field like pubDate and/or

相关标签:
2条回答
  • 2021-02-04 19:39

    to convert PublishDate in RSS to your computer datetime you could write this lines

      string dateStr = item.PublishDate.ToString("ffffd MMM dd HH:mm:ss zzzz yyyy");
                        DateTime PostDate = DateTime.ParseExact(dateStr, "ffffd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture);
    
    0 讨论(0)
  • 2021-02-04 19:44

    Here is my hacky workaround 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);
    
    0 讨论(0)
提交回复
热议问题