MSXML DLL : #import <msxml3.dll>
I am writing XML using MSXML DOM & expected to add multiple processing instruction.
ex:

<?xml version="1.0" encoding="ISO-8859-1"?>          ------ first processing instruction.
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>  ------ second processing instruction.
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
</catalog>

I have write below code using MSXML DOM. But it failed to add first & second processing instruction.

MSXML2::IXMLDOMElementPtr pXMLRootElem; 
        MSXML2::IXMLDOMNodePtr pTestDOMNodePtr ;
        //Create an instance of the DOMDocument object:
        m_docPtr.CreateInstance(__uuidof(MSXML2::DOMDocument30));
        char* xmlfile =  (char*)xmlFileFullPath.c_str();
        _variant_t varXml(xmlfile); //XML file to load
        m_docPtr->async = VARIANT_FALSE;
        m_docPtr->validateOnParse = VARIANT_FALSE;
        m_docPtr->resolveExternals = VARIANT_FALSE;
        //load XML file.
        if(m_docPtr->loadXML(_T("<catalog><cd></cd></catalog>")) == VARIANT_FALSE)
        {
            CCommonFunction::log ("Failed to create the XML file.");
            return false;
        }
        //Get the root element just created    
        pXMLRootElem = m_docPtr->GetdocumentElement();
        //// Add first ProcessingInstruction <?xml version="1.0" encoding="ISO-8859-1"?>
        MSXML2::IXMLDOMProcessingInstructionPtr pXMLProcessingNode =  
              m_docPtr->createProcessingInstruction("xml", " version='1.0' encoding='UTF-8'");
        _variant_t vtObject;
        vtObject.vt = VT_DISPATCH;
        vtObject.pdispVal = pXMLRootElem;
        vtObject.pdispVal->AddRef();
        m_docPtr->insertBefore(pXMLProcessingNode,vtObject);

        //// Add second ProcessingInstruction <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
        MSXML2::IXMLDOMProcessingInstructionPtr pXSLTNode =  
        m_docPtr->createProcessingInstruction("xml-stylesheet", "type='text/xsl' href='cdcatalog.xsl'");
        m_docPtr->insertBefore(pXSLTNode,vtObject);

Please help in understanding why MSXML is not adding multiple processing instruction ?

I found the solution.

//Add first processing instruction
MSXML2::IXMLDOMProcessingInstructionPtr ptrPI = m_docPtr->createProcessingInstruction(L"xml", L"version='1.0' encoding='UTF-8'");

m_docPtr->insertBefore(ptrPI, m_docPtr->documentElement.GetInterfacePtr());

//Add second processing instruction
MSXML2::IXMLDOMProcessingInstructionPtr ptrPI1 = m_docPtr->createProcessingInstruction(L"xml-stylesheet", L"type='text/xsl' href='TestCaseFrameWork.xsl");

m_docPtr->insertBefore(ptrPI1, m_docPtr->documentElement.GetInterfacePtr());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.