本文介绍了忽略DOCTYPE .dtd,但是.dtd文件必须仍然存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理HTTP请求的Web服务。它收到的文档具有指定.dtd文件的嵌入DOCTYPE。我希望使用一个更新的XML模式验证文件,该文件是为较新的设备连接到我的服务而制作的。

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.

我可以成功忽略.dtd文件中进行的验证,但是.dtd文件必须存在于我的本地硬盘上。我想删除这些过时的文件,并且还没有找到解决方法。

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.

我正在处理的示例XML文档:

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!
}

捕获的异常:


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必须存在,否则会引发异常。

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.

我正在运行在Windows 7上,使用Visual Studio 2010和.Net 4.0

I am running on Windows 7, using Visual Studio 2010 and .Net 4.0

如何忽略嵌入的.dtd,也不需要在计算机上安装虚拟.dtd文件?

How can I ignore the embeded .dtd and also not require a dummy .dtd file to be installed on my computer?

推荐答案

解决方案是将基础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

这是已更正的函数:

private void LoadXmlDoc(XmlTextReader myXmlTextReader)
{
    // The next line is the fix!!!
    myXmlTextReader.XmlResolver = null;  // Don't require file in system32\inetsrv

    XmlReaderSettings readerSettings = new XmlReaderSettings();
    readerSettings.ValidationType = ValidationType.Schema;
    readerSettings.Schemas.Add(null, MyGoodSchemaFile);
    readerSettings.DtdProcessing = DtdProcessing.Ignore;
    readerSettings.XmlResolver = null; // Doesn't help

    readerSettings.ValidationEventHandler += ValidationEventHandle;
    XmlReader myXmlReader = XmlReader.Create(myXmlTextReader, readerSettings);

    XmlDocument myXmlDocument = new XmlDocument();
    myXmlDocument.XmlResolver = null; // Doesn't help
    myXmlDocument.Load(myXmlReader); // Load doc, no .dtd required on local disk
}

这篇关于忽略DOCTYPE .dtd,但是.dtd文件必须仍然存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:28