Search code examples
c++xmllite

XMLLite ignore encoding


I have to read XML files in C++ and we are using XMLLite the problem I have is that the first line of my XML specifies the encoding

<?xml version="1.0" encoding="ISO-8859-15"?>

and when I try to parse the file with XMLLite, I'm getting the error MX_E_ENCODINGSIGNATURE. If I remove the encoding part, the XML parses fine.

<?xml version="1.0"?>

So is it possible to programmatically tell XMLLite to ignore the encoding resp. what other options do I have?

One solution would be to write my own input stream class and suppress this header, and inject the short version, but it would be nicer to have a clean solution.


Solution

  • Even though the documentation says that XMLLite can not handle different encodings it seems that you can go around this by using IMultiLanguage2, which is mentioned in the documentation but no example is given.

    So here is how to enable it:

    #include <mlang.h>
    
    XMLLiteReader::XMLLiteReader(void)
    {
        mLanguage = NULL;
        mXMLLiteReader = NULL;
        mCOMInitialized = false;
    
        HRESULT hr;
        if(CoInitialize(NULL) != S_OK)
            return;
        mCOMInitialized = true;
    
        if((hr = CoCreateInstance(CLSID_CMultiLanguage, NULL, CLSCTX_ALL, IID_IMultiLanguage2, (void **)&mLanguage)) != S_OK)
            return;
    
        hr = CreateXmlReader(__uuidof(IXmlReader), (void**) &mXMLLiteReader, NULL);
        if(hr != S_OK)
        {
            mXMLLiteReader = NULL;
            return;
        }
    
        hr = mXMLLiteReader->SetProperty(XmlReaderProperty_MultiLanguage, (LONG_PTR)mLanguage);
    }