忽略 DOCTYPE .dtd,但 .dtd 文件必须仍然存在
我有一个处理 HTTP 请求的 Web 服务。它收到的文档具有指定 .dtd 文件的嵌入 DOCTYPE。我希望在较新的设备连接到我的服务时使用较新的 XML 架构验证文件。
我可以成功忽略 .dtd 文件中进行的验证,但 .dtd 文件必须存在于我的本地硬盘驱动器上。我想删除这些过时的文件,还没有找到方法。
我正在处理的示例 XML 文档:
<?xml version="1.0" encoding="us-ascii" standalone="no"?>
<!DOCTYPE SomeMessage SYSTEM "SomeMessage.dtd">
<SomeMessage>data</SomeMessage>
我用来打开文档的函数:
private void LoadXmlDoc(XmlTextReader myXmlTextReader)
{
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationType = ValidationType.Schema;
readerSettings.Schemas.Add(null, MyGoodSchemaFile);
readerSettings.DtdProcessing = DtdProcessing.Ignore;
readerSettings.XmlResolver = null; // Added as a test.
readerSettings.ValidationEventHandler += ValidationEventHandle;
XmlReader myXmlReader = XmlReader.Create(myXmlTextReader, readerSettings);
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.XmlResolver = null; // Added as a test.
myXmlDocument.Load(myXmlReader); // Exception thrown here!
}
捕获的异常:
System.IO.FileNotFoundException: Could not find file 'c:\windows\system32\inetsrv\SomeMessage.dtd'. File name: 'c:\windows\system32\inetsrv\SomeMessage.dtd' at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
SomeMessage.dtd 文件的内容未使用 - 它按照我的意愿被忽略。但是,虚拟文件“c:\windows\system32\inetsrv\SomeMessage.dtd”必须存在,否则会引发异常。
我在 Windows 7 上运行,使用 Visual Studio 2010 和 .Net 4.0
如何忽略嵌入的 .dtd 并且不需要在我的计算机上安装虚拟 .dtd 文件?
I have a web service that processes an HTTP request. The document it receives has an embeded DOCTYPE that specifies a .dtd file. I wish to use a newer XML schema validation file made for when newer devices connect to my service.
I can successfully ignore the validation that goes on in the .dtd file, but the .dtd file must exist on my local hard drive. I want to remove these obsolete files, and have not found a way.
Sample XML document I am processing:
<?xml version="1.0" encoding="us-ascii" standalone="no"?>
<!DOCTYPE SomeMessage SYSTEM "SomeMessage.dtd">
<SomeMessage>data</SomeMessage>
The function that I am using to open the document:
private void LoadXmlDoc(XmlTextReader myXmlTextReader)
{
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationType = ValidationType.Schema;
readerSettings.Schemas.Add(null, MyGoodSchemaFile);
readerSettings.DtdProcessing = DtdProcessing.Ignore;
readerSettings.XmlResolver = null; // Added as a test.
readerSettings.ValidationEventHandler += ValidationEventHandle;
XmlReader myXmlReader = XmlReader.Create(myXmlTextReader, readerSettings);
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.XmlResolver = null; // Added as a test.
myXmlDocument.Load(myXmlReader); // Exception thrown here!
}
The exception that is caught:
System.IO.FileNotFoundException: Could not find file 'c:\windows\system32\inetsrv\SomeMessage.dtd'. File name: 'c:\windows\system32\inetsrv\SomeMessage.dtd' at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
The content of the SomeMessage.dtd file is not used--it is ignored as I desire. However, the dummy file "c:\windows\system32\inetsrv\SomeMessage.dtd" must exist, otherwise the exception is thrown.
I am running on Windows 7, using Visual Studio 2010 and .Net 4.0
How can I ignore the embeded .dtd and also not require a dummy .dtd file to be installed on my computer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是将底层XmlTextReader 的XmlResolver 设置为null。更改 XmlReaderSettings.XmlResolver=null 没有帮助,设置 XmlDocument.XmlResolver=null 也没有帮助
以下是更正后的函数:
The solution is to set the underlying XmlTextReader's XmlResolver to null. Changing the XmlReaderSettings.XmlResolver=null did not help, nor did setting the XmlDocument.XmlResolver=null
Here is the corrected function: