visual-c++tinyxml2

Why is using tinyxml2-ex::text returning corrupted text?


I am trying to use the tinyxml2-ex library to read some XML data.

When I try using it's specific API call:

const CString strNameToUse(tinyxml2::text(pAssign).c_str());

The resulting string loses things like accents. In the end I have reverted to my original approach with the UTF8 handling:

const CString strNameToUse(CA2CT(pAssign->GetText(), CP_UTF8));

This works fine. Does anyone know why the tinyxml2-ex::text approach fails? Note that it is permissible to the use the tinyxml2 namespace.


The referred to library is using std::string and does it like this:

// helper function to get element text as a string, blank if none
inline std::string text (const XMLElement * element)
{
    if (!element)
        throw XmlException ("null element"s);

    if (auto value = element -> GetText())
        return std::string (value);
    else
        return ""s;
}


Solution

  • The library author explained (GitHub discussion:

    It's because tixml2ex::text (see line 465 in tixml2ex.h) does this:

    if (auto value = element -> GetText())
        return std::string (value);
    

    which will corrupt any string containing characters outside ASCII 127.