C#中RSS feed中的DateTime解析异常

C#中RSS feed中的DateTime解析异常,第1张

概述我正在尝试使用SyndicationFeedFormatter和SyndicationFeed对象解析Rss2,Atom提要.但是我在解析DateTime字段时得到了XmlExceptions,比如pubDate和/或lastBuildDate. 2010年2月24日星期三18:56:04 GMT 00:00不起作用 2010年2月24日星期三18:56:04 GMT工作 因此,由于时区字段,它正 我正在尝试使用SyndicationFeedFormatter和SyndicationFeed对象解析RSS2,Atom提要.但是我在解析DateTime字段时得到了XmlExceptions,比如pubDate和/或lastBuildDate.

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

2010年2月24日星期三18:56:04 GMT工作

因此,由于时区字段,它正在投掷.

作为一种解决方法,对于熟悉的提要,我会手动修复这些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 Feed:http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&q=test&cf=all&output=rss

例外情况:

XmlException Error in line 1 position
376. An error was encountered when parsing a DateTime value in the XML.
at
System.ServiceModel.Syndication.RSS20FeedFormatter.DateFromString(String
dateTimeString,XmlReader reader)
at
System.ServiceModel.Syndication.RSS20FeedFormatter.readxml(XmlReader
reader,SyndicationFeed result) at
System.ServiceModel.Syndication.RSS20FeedFormatter.ReadFrom(XmlReader
reader) at … 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>

有没有更好的方法来实现这一目标?请帮忙.谢谢.

解决方法 以下是我阅读@R_403_5561@新闻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);
总结

以上是内存溢出为你收集整理的C#中RSS feed中的DateTime解析异常全部内容,希望文章能够帮你解决C#中RSS feed中的DateTime解析异常所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1262743.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-08
下一篇2022-06-08

发表评论

登录后才能评论

评论列表(0条)

    保存