【问题标题】:Error Loading RSS XML: Element 'channel' with namespace name '' was not found.加载 RSS XML 时出错:找不到名称空间名称为“”的元素“通道”。
【发布时间】:2013-04-01 15:45:58
【问题描述】:

我这里有个例外

var stream = e.Result;
var response = XmlReader.Create(stream);
var feeds = SyndicationFeed.Load(response); // IT IS HERE

例外情况

未找到命名空间名称为“”的元素“通道”。 8号线, 位置 2。

RSS 看起来像:

 <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
 <atom:link href="http://dallas.example.com/rss.xml" rel="self"
 type="application/rss+xml" /> <channel> <title>News</title>
 <link>http://www.samsung.com/us</link> <description>News</description>
 ...

http://validator.w3.org/feed/ 说“这是一个有效的 RSS 提要”。 (你可以在这里查看http://validator.w3.org/feed/check.cgi?url=http%3A%2F%2Fwww.samsung.com%2Fus%2Ffunction%2Frss%2FrssFeedItemList.do%3FctgryCd%3D101%26typeCd%3DNEWS

所以我不知道发生了什么...... :(

我们可以解决方法来抑制SyndicationFeed class 的某些验证消息吗?

感谢您提供任何让我有机会忘记此异常的解决方案!

【问题讨论】:

    标签: c# .net rss


    【解决方案1】:

    如果您查看您列出的 W3 验证的结果,内容如下:

    line 8, column 0: Undocumented use of atom:link 
    

    atom:link 元素放在channel 元素之前会导致SyndicationFeed 类在加载时失败。您可以通过在本地下载 rss 提要 xml、删除/注释 atom:link 行并再次运行您的代码来自行测试。如果没有该行,则会加载 xml 并找到提要。这有 happened beforeSyndicationFeed 类。

    【讨论】:

    • 谢谢!你知道关于atom:link 的规范是什么吗?它应该在那里吗?
    • 即使 W3 验证认为提要“有效”,SyndicationFeed.Load() 方法还是有点脆弱,并希望 atom:link 标记位于其他位置,例如作为 @987654336 的子元素@ 元素。您可以查看AtomRSS 规范以了解有关元素放置的更多详细信息。
    • 另外...查看 RSS 顾问委员会的 atom:link Recommendations 部分,您会发现可以接受的位置在 channel 内。
    • 酷。我还发现了这个feedvalidator.org/docs/warning/MissingAtomSelfLink.html,上面写着:然后在频道部分中将atom:link 插入到您的提要中。下面是一个帮助您入门的示例。请务必将 href 属性的值替换为供稿的网址。
    【解决方案2】:

    感谢Michael’s answer,我能够预处理有问题的 XML(不在我的控制之下)以移动错误的atom:link 元素:

    private static readonly XName AtomLink = XName.Get( "link", "http://www.w3.org/2005/Atom" );
    private static readonly XName Channel = XName.Get( "channel" );
    
    ...
    var document = XDocument.Load( stream );
    var channel = document.Root.Element( Channel );
    foreach( var misplacedLink in document.Root.Elements( AtomLink ) ) {
        misplacedLink.Remove( );
        channel.Add( misplacedLink );
    }
    
    using( var reader = document.CreateReader( ) )
        return SyndicationFeed.Load( reader );
    

    【讨论】:

      猜你喜欢
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      相关资源
      最近更新 更多