For performance and security reasons , Microsoft does not usually allow <!DOCTYPE>
(aka Document Type Definition ). Because of this, you must use the loadXML
method to set <!DOCTYPE>
. Therefore, it cannot be installed after creating or importing a document.
Also, because of the default security settings in MSXML6, you usually cannot import XML with <!DOCTYPE>
. Therefore, you must disable the ProhibitDTD
setting of the object.
Edit: You should be aware that HTML5 is not XML . In addition, <!DOCTYPE>
is considered optional for XHTML5 .
First, start with the desired exit.
<!DOCTYPE html> <html />
Based on the syntax, I assume that you are using C # and have added a link to msxml6.dll
. The following code will allow you to create these two processing instructions.
MSXML2.DOMDocument60 doc = new MSXML2.DOMDocument60(); // Disable validation when importing the XML doc.validateOnParse = false; // Enable the ability to import XML that contains <!DOCTYPE> doc.setProperty("ProhibitDTD", false); // Perform the import doc.loadXML("<!DOCTYPE html><html />"); // Display the imported XML Console.WriteLine(doc.xml);
Here is also a copy of the code written in VBScript.
Set doc = CreateObject("MSXML2.DOMDocument.6.0") ' Disable validation when importing the XML doc.validateOnParse = False ' Enable the ability to import XML that contains <!DOCTYPE> doc.setProperty "ProhibitDTD", false ' Perform the import doc.loadXML "<!DOCTYPE html><html />" ' Display the imported XML WScript.Echo objXML.xml
Finally, here is a copy of the code written in C ++.
#include <stdio.h> #include <tchar.h> #include <msxml6.h> #import <msxml6.dll> #pragma comment(lib, "msxml6.lib") int _tmain(int argc, _TCHAR* argv[]) { HRESULT hr; // IXMLDOMDocument2 is needed for setProperty MSXML2::IXMLDOMDocument2 *doc; // Initialize COM hr = CoInitialize(NULL); if (SUCCEEDED(hr)) { // Create the object hr = CoCreateInstance(CLSID_DOMDocument60, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument2, (void**)&doc); if (SUCCEEDED(hr)) { // Disable validation when importing the XML doc->validateOnParse = VARIANT_FALSE; // Enable the ability to import XML that contains <!DOCTYPE> doc->setProperty(_bstr_t(L"ProhibitDTD"), VARIANT_FALSE); // Perform the import doc->loadXML(_bstr_t(L"<!DOCTYPE html><html />")); // Display the imported XML MessageBox(NULL, doc->xml, NULL, 0); } // Cleanup COM CoUninitialize(); } return 0; }