c# - RSS 提要中 DateTime 解析的异常在 c# 中使用 SyndicationFeed

标签 c# rss

我正在尝试使用 SyndicationFeed 对象解析 Rss2、Atom 提要。但是我在解析诸如 pubDate 之类的 DateTime 字段时遇到了 XmlExceptions

2012-01-17 08:01:06

public static List<SyndicationItem> getRssData(string url)
{
    List<SyndicationItem> list = new List<SyndicationItem>();

    WebClient client = new WebClient();
    try
    {
        SyndicationFeed feed = SyndicationFeed.Load(XmlReader.Create(url));
        list = (from item in feed.Items select item).ToList();
    }
    catch (Exception e)
    {
        throw e;
    }

    return list;
}

网址链接http://news.163.com/special/00011K6L/rss_newstop.xml

<item id="2">
    <title>...</title>
    <link>...</link>
    <description>......</description>
    <pubDate>2012-01-17 12:09:29</pubDate><-----Exception
</item>

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

最佳答案

有一个解决方法 RSS20FeedFormatter throws exception trying to read some DateTime formats .

To work around this problem, create a custom XML reader that recognizes different date formats. The following is an example of a custom XML reader:

XmlReader r = new MyXmlReader(url);
SyndicationFeed feed = SyndicationFeed.Load(r);
Rss20FeedFormatter rssFormatter = feed.GetRss20Formatter();
XmlTextWriter rssWriter = new XmlTextWriter("rss.xml", Encoding.UTF8);
rssWriter.Formatting = Formatting.Indented;
rssFormatter.WriteTo(rssWriter);
rssWriter.Close();

..和前面代码中使用的类:

class MyXmlReader : XmlTextReader
{
    private bool readingDate = false;
    const string CustomUtcDateTimeFormat = "ddd MMM dd HH:mm:ss Z yyyy"; // Wed Oct 07 08:00:07 GMT 2009

    public MyXmlReader(Stream s) : base(s) { }

    public MyXmlReader(string inputUri) : base(inputUri) { }

    public override void ReadStartElement()
    {
        if (string.Equals(base.NamespaceURI, string.Empty, StringComparison.InvariantCultureIgnoreCase) &&
            (string.Equals(base.LocalName, "lastBuildDate", StringComparison.InvariantCultureIgnoreCase) ||
            string.Equals(base.LocalName, "pubDate", StringComparison.InvariantCultureIgnoreCase)))
        {
            readingDate = true;
        }
        base.ReadStartElement();
    }

    public override void ReadEndElement()
    {
        if (readingDate)
        {
            readingDate = false;
        }
        base.ReadEndElement();
    }

    public override string ReadString()
    {
        if (readingDate)
        {
            string dateString = base.ReadString();
            DateTime dt;
            if(!DateTime.TryParse(dateString,out dt))
                dt = DateTime.ParseExact(dateString, CustomUtcDateTimeFormat, CultureInfo.InvariantCulture);
            return dt.ToUniversalTime().ToString("R", CultureInfo.InvariantCulture);
        }
        else
        {
            return base.ReadString();
        }
    }
}

关于c# - RSS 提要中 DateTime 解析的异常在 c# 中使用 SyndicationFeed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8891047/

相关文章:

c# - 无法使用 R(D)COM 自动读取 csv

c# - 本地主机永远等待 ASP.NET 5 应用程序模板项目

c# - 如何设计两组数据交集的sql查询

php - 解析 rss feed、将其插入 mysql 数据库然后使用 ajax 异步访问的最佳途径是什么?

iPhone RSS 阅读器——parseXML 不会加载某些 XML 提要

python - 如何在 Python 中解析 RSS feed 中的 HTML 标签

c# - XAML 设计器显示属性名称而不是设计器数据 (d :DataContext) when Binding

c# - 创建一个查询并将结果放入数据网格

也处理 FeedBurner 的 Python RSS 解析器

ios - 自定义 UITableView - iOs