Ignore DOCTYPE.dtd, but the .dtd file must still exist.

I have a web service processing an HTTP request. The resulting document has a built-in DOCTYPE that indicates the .dtd file. I want to use the new XML Schema Validation file created to connect new devices to my service.

I can successfully ignore the verification that continues in the .dtd file, but the .dtd file must exist on my local hard drive. I want to delete these obsolete files and have not found a way.

Sample XML document that I am processing:

<?xml version="1.0" encoding="us-ascii" standalone="no"?> <!DOCTYPE SomeMessage SYSTEM "SomeMessage.dtd"> <SomeMessage>data</SomeMessage> 

Function I use to open a 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! } 

Excluded:

  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 contents of the SomeMessage.dtd file are not used - it is ignored as I wish. However, the dummy file "c: \ windows \ system32 \ inetsrv \ SomeMessage.dtd" must exist, otherwise an exception will be thrown.

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

How can I ignore the built-in .dtd and also not require the installation of a dummy .dtd file on my computer?

+6
c # xml dtd
source share
1 answer

The solution is to set the base XmlTextReader XmlResolver to null. Changing XmlReaderSettings.XmlResolver = null did not help, nor did it set XmlDocument.XmlResolver = null

Here is the corrected function:

 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 } 
+9
source share

All Articles