c++msxml4

MSXML4 and setting the encoding string


I using MSXML4 to generate a XML.

I'm trying to set the encoding value to UTF-8.

Here is my code:

const _bstr_t k_XML_Tag_Name ("xml");
const _bstr_t k_Processing_Tag_Name ("version=\"1.0\" encoding=\"utf-8\"");

MSXML2::IXMLDOMProcessingInstructionPtr pProccessingInstruction = m_pXmlDoc->createProcessingInstruction(k_XML_Tag_Name, k_Processing_Tag_Name);

HRESULT result = m_pXmlDoc->appendChild(pProccessingInstruction);

result is always S_FALSE and the preprocessing string is:

<?xml version="1.0"?>

So, why isn't the encoding string showing up?


Solution

  • When you save the xml to a file you should see the <?xml version="1.0" encoding="UTF-8"?> declaration. However, according to this article, when you use the XML property of the document the declaration returned does not contain the encoding, which seems to be by design. Is this the behavior you are encountering? The article states that:

    This is normal. The reason it did this is so that you can turn around and call LoadXML with this string and it will work. If it does not do this, LoadXML will fail with the error message: "Switch from current encoding to specified encoding not supported."