阅读 RSS 问题 - 未找到预期的 DTD 标记
我正在打开引用 DTD 的 XML 文件,如下所示:
<?xml version="1.0" encoding="windows-1250"?>
<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"
"http://my.netscape.com/publish/formats/rss-0.91.dtd">
这是 C# 代码的一部分:
public static XmlDocument FromUri(string uri)
{
XmlDocument xmlDoc;
WebClient webClient = new WebClient();
using (Stream rssStream = webClient.OpenRead(uri))
{
XmlTextReader reader = new XmlTextReader(rssStream);
xmlDoc = new XmlDocument();
xmlDoc.XmlResolver = null;
xmlDoc.Load(reader);
}
return xmlDoc;
}
当我尝试加载“阅读器”时,出现以下错误:未找到预期的 DTD 标记。 有没有办法让解析器忽略 Doctype 元素?或者也许,我可以做一些更有效率的事情?
I am opening XML file that refers to a DTD as follows:
<?xml version="1.0" encoding="windows-1250"?>
<!DOCTYPE rss PUBLIC "-//Netscape Communications//DTD RSS 0.91//EN"
"http://my.netscape.com/publish/formats/rss-0.91.dtd">
Here is the part of C# code:
public static XmlDocument FromUri(string uri)
{
XmlDocument xmlDoc;
WebClient webClient = new WebClient();
using (Stream rssStream = webClient.OpenRead(uri))
{
XmlTextReader reader = new XmlTextReader(rssStream);
xmlDoc = new XmlDocument();
xmlDoc.XmlResolver = null;
xmlDoc.Load(reader);
}
return xmlDoc;
}
When I try to Load 'reader' I get the following error: Expected DTD markup was not found.
Is there any way to get the parser to ignore the Doctype element? Or maybe, I can do something more efficient?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要 DTD 没有定义任何您需要使用的
&entities;
(请使用字符引用!),您就可以通过设置告诉 XmlTextReader 不要包含外部实体(包括 DTD) XmlResolver 到无效的。(这实际上应该是默认设置。大多数时候,您在阅读 XML 文档时,您不希望它去下载 DTD,即使 DTD 仍然存在。在这种情况下,AOL 的表现尤其糟糕,不仅是因为删除 DTD,但对某些 HTML 提供错误的 301 响应,而不是适当的 404。)
As long as the DTD doesn't define any
&entities;
that you need to use (use character references instead!), you can tell XmlTextReader not to include external entities (including the DTD) by setting XmlResolver to null.(This should have been the default really. Most times you're reading an XML document you don't want it heading off to download a DTD, even when the DTD is still present. In this case AOL have behaved particularly badly by not only removing the DTD, but serving an incorrect 301 response to some HTML instead of the appropriate 404.)
http://my.netscape.com/publish/formats/rss-0.91。 dtd 导致 301,进而转到 http://netscape.aol。 com/index.html
即此 URL 没有 DTD。
http://my.netscape.com/publish/formats/rss-0.91.dtd leads to a 301 that, in turn, goes to http://netscape.aol.com/index.html
i.e. There is no DTD at this URL.