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

后端 未结 1 1354
时光说笑
时光说笑 2020-12-19 09:16

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

相关标签:
1条回答
  • 2020-12-19 09:35

    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:

    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
    }
    
    0 讨论(0)
提交回复
热议问题