如何确定 .NET SyndicatedFeed 的提要类型(RSS 与 Atom)?

发布于 2024-09-04 20:09:38 字数 319 浏览 7 评论 0原文

是否可以从 System.ServiceModel.Syndicate 确定。 SyndicateFeed 实例正在读取什么类型的 SyndicateFeed?如果我拥有的只是 url (blahblah.com/feed),它可能是 rss 或atom,并且根据我想要做的一件事或另一件事的类型。

有没有一种简单的方法可以判断,无需解析文档并查找特定字符?

Is it possible to determine from the System.ServiceModel.Syndication.SyndicationFeed instance what type of SyndicationFeed is being read? If all I have is the url (blahblah.com/feed) it might be rss or atom, and depending on the type I want to do one thing or the other.

Is there a simple way to tell without parsing the document and looking for specific characters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2024-09-11 20:09:38

老问题,但值得回答。

有一种相对简单的方法来确定您是否拥有 RSS 或 Atom 提要。它确实需要阅读或尝试阅读该文档。

public SyndicationFeed GetSyndicationFeedData(string urlFeedLocation)
{
    XmlReaderSettings settings = new XmlReaderSettings
        {
            IgnoreWhitespace = true,
            CheckCharacters = true,
            CloseInput = true,
            IgnoreComments = true,
            IgnoreProcessingInstructions = true, 
            //DtdProcessing = DtdProcessing.Prohibit // .NET 4.0 option
        };

    if (String.IsNullOrEmpty(urlFeedLocation))
        return null;

    using (XmlReader reader = XmlReader.Create(urlFeedLocation, settings))
    {
        if (reader.ReadState == ReadState.Initial)
            reader.MoveToContent();

        // now try reading...

        Atom10FeedFormatter atom = new Atom10FeedFormatter();
        // try to read it as an atom feed
        if (atom.CanRead(reader))
        {
            atom.ReadFrom(reader);
            return atom.Feed;
        }

        Rss20FeedFormatter rss = new Rss20FeedFormatter();
        // try reading it as an rss feed
        if (rss.CanRead(reader))
        {
            rss.ReadFrom(reader);
            return rss.Feed;
        }

        // neither?
        return null;
    }
}

Old question, but it deserves an answer.

There is a relatively simple way to determine if you've got an RSS or an Atom feed. It does require reading, or trying to read the document.

public SyndicationFeed GetSyndicationFeedData(string urlFeedLocation)
{
    XmlReaderSettings settings = new XmlReaderSettings
        {
            IgnoreWhitespace = true,
            CheckCharacters = true,
            CloseInput = true,
            IgnoreComments = true,
            IgnoreProcessingInstructions = true, 
            //DtdProcessing = DtdProcessing.Prohibit // .NET 4.0 option
        };

    if (String.IsNullOrEmpty(urlFeedLocation))
        return null;

    using (XmlReader reader = XmlReader.Create(urlFeedLocation, settings))
    {
        if (reader.ReadState == ReadState.Initial)
            reader.MoveToContent();

        // now try reading...

        Atom10FeedFormatter atom = new Atom10FeedFormatter();
        // try to read it as an atom feed
        if (atom.CanRead(reader))
        {
            atom.ReadFrom(reader);
            return atom.Feed;
        }

        Rss20FeedFormatter rss = new Rss20FeedFormatter();
        // try reading it as an rss feed
        if (rss.CanRead(reader))
        {
            rss.ReadFrom(reader);
            return rss.Feed;
        }

        // neither?
        return null;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文