如何用.NET控制DTD读取?
我们的 AC# 程序读取 XML 文件。 XML 文件具有standalone='no' 标头。
DOCTYPE 过去看起来像:
<!DOCTYPE foo SYSTEM "silly.dtd">
其中silly.dtd 并不位于文件旁边。
由于各种原因,我将其更改为
<!DOCTYPE foo PUBLIC "-//Some Public Id" "urn:outerspace:silly.dtd">
我希望没有任何更改,因为 DTD 以前无法以“./silly.dtd”打开,现在也无法以“urn:outerspace:silly.dtd”打开。唯一的区别是目录解析器不必担心系统 ID 的绝对化。
想象一下我惊讶地发现 .NET 运行时出现异常,显然是在尝试打开 urn: 地址处的 DTD。
有好心人可以指导我找到一个方法,让 .NET 在这种情况下静静地放弃吗?我知道如何在 Java 中做到这一点,但在 .NET 中我有点迷失。
A C# program of ours reads an XML file. The XML file has standalone='no' header.
The DOCTYPE used to look like:
<!DOCTYPE foo SYSTEM "silly.dtd">
where silly.dtd is not sitting right there next to the file.
For various reasons, I changed this to
<!DOCTYPE foo PUBLIC "-//Some Public Id" "urn:outerspace:silly.dtd">
I expected nothing to change, since the DTD could not be opened as './silly.dtd' before, and it can't be opened at 'urn:outerspace:silly.dtd' now. The only difference was that a catalog resolver wouldn't have to worry about the absolutization of the system ID.
Imagine my surprise to get an exception from the .NET runtime, apparently trying to open the DTD at the urn: address.
Can some kind person direct me to a recipe to tell .NET to just quietly give up in this case? I know how to do this in Java, but in .NET I'm a bit lost.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您具有以下属性,读者将不会尝试访问 dtd:
.ProhibitDtd=false;
.XmlResolver=null;
The reader will not attempt to access the dtd if you have the following properties:
.ProhibitDtd=false;
.XmlResolver=null;
实现您自己的 XmlResolver 来处理此问题(您可以只需继承 XmlUrlResolver),然后将其插入 XmlTextReader 和将读取器传递到 XmlDocument。
Implement your own XmlResolver to handle this (you could just inherit from XmlUrlResolver), then plug it into a XmlTextReader and pass the reader to the XmlDocument.