6

SyndicationFeedFormatter および SyndicationFeed オブジェクトを使用して、Rss2、Atom フィードを解析しようとしています。しかし、pubDate や lastBuildDate などの DateTime フィールドを解析しているときに XmlExceptions が発生します。

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

例外の詳細:

XmlException 行 1 の位置 376 でエラーが発生しました。XML で DateTime 値を解析中にエラーが発生しました。
System.ServiceModel.Syndication.Rss20FeedFormatter.DateFromString (文字列 dateTimeString、XmlReader リーダー)
で System.ServiceModel.Syndication.Rss20FeedFormatter.ReadXml (XmlReader リーダー、SyndicationFeed 結果) で System.ServiceModel.Syndication.Rss20FeedFormatter.ReadFrom (XmlReader リーダー) で。 .. cs: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>

これを達成するためのより良い方法はありますか?助けてください。ありがとう。

4

2 に答える 2

10

これは、Google ニュースの RSS フィードを読むためのハックな回避策です。

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);
于 2010-10-14T19:16:41.437 に答える
0

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);
于 2015-08-28T01:42:25.063 に答える