【问题标题】:Exceptions with DateTime parsing in RSS feed in C#C# 中 RSS 提要中的 DateTime 解析异常
【发布时间】:2010-02-24 19:16:17
【问题描述】:

我正在尝试使用 SyndicationFeedFormatter 和 SyndicationFeed 对象解析 Rss2、Atom 提要。但是在解析 pubDate 和/或 lastBuildDate 之类的 DateTime 字段时,我得到了 XmlExceptions。

2010 年 2 月 24 日星期三 18:56:04 GMT+00:00 不起作用

格林威治标准时间 2010 年 2 月 24 日星期三 18:56:04 有效

所以,由于时区字段,它正在抛出。

作为一种解决方法,对于熟悉的提要,我将手动修复那些 DateTime 节点 - 通过捕获 XmlException、将 Rss 加载到 XmlDocument、修复这些节点的值、创建新的 XmlReader,然后从这个新的 XmlReader 对象返回格式化程序(代码未显示)。但是要使这种方法起作用,我需要事先知道哪些节点会导致异常。

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

异常详情:

第 1 行位置出现 XmlException 错误 376. 解析 XML 中的 DateTime 值时遇到错误。
在 System.ServiceModel.Syndication.Rss20FeedFormatter.DateFromString(字符串 dateTimeString, XmlReader 阅读器)
在 System.ServiceModel.Syndication.Rss20FeedFormatter.ReadXml(XmlReader 阅读器,SyndicationFeed 结果)在 System.ServiceModel.Syndication.Rss20FeedFormatter.ReadFrom(XmlReader 读者)在... 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>

有没有更好的方法来实现这一点?请帮忙。谢谢。

【问题讨论】:

    标签: exception datetime parsing rss xmlexception


    【解决方案1】:

    这是我阅读 Google 新闻 RSS 提要的 hacky 解决方法。

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

    【讨论】:

    • 很好的修复。我讨厌我们必须这样做。我的 PHP 朋友在我谈到一个服务会因为像这样的小事而抛出错误时,也会侧目看着我。
    • 这是个好主意,但是如果字符串不是 00:00,如果是另一个时区怎么办?我认为最好在这里使用 Regex 而不是 Replace
    【解决方案2】:

    要将 RSS 中的 PublishDate 转换为您的计算机日期时间,您可以编写此行

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-01
      • 2022-12-17
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多