3

我这里有个例外

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 类的一些验证消息吗?

感谢您提供任何让我有机会忘记这个例外的解决方案!

4

2 回答 2

4

如果您查看您列出的 W3 验证的结果,它会显示:

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

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

于 2013-04-10T02:27:46.963 回答
1

感谢迈克尔的回答,我能够预处理有问题的 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 );
于 2019-07-17T00:36:07.650 回答