忽略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:windowssystem32inetsrvSomeMessage.dtd'.
File name: 'c:windowssystem32inetsrvSomeMessage.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文件?


解决方法是将基础XmlTextReader的XmlResolver设置为null。 更改XmlReaderSettings.XmlResolver = null没有帮助,也没有设置XmlDocument.XmlResolver = null

这是更正后的功能:

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

    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
}
链接地址: http://www.djcxy.com/p/34897.html

上一篇: Ignore DOCTYPE .dtd, but .dtd file must still exist

下一篇: Path to XML DTD for DBUnit in multi