When you use the SQLBindCol() function to bind XML columns in a query result-set to application variables, you can specify the data type of the application variables as SQL_C_BINARY, SQL_C_CHAR, SQL_C_DBCHAR or SQL_C_WCHAR. When you select data from XML columns in a table by using the SQLBindCol() function, the output is returned as the serialized data.
When you are retrieving a result-set from an XML column, you can bind your application variable to the SQL_C_BINARY type to avoid possible data corruption. Binding to character types can result in possible data corruption as result of a code page conversion. Data corruption can occur when characters in the source code page cannot be represented in the target code page.
An implicit serialization of data takes place on the database server before XML data is sent to the application. You can explicitly serialize the XML data to a specific data type by calling the XMLSERIALIZE() function. However, the explicitly serializing to character types with the XMLSERIALIZE() function can introduce encoding issues.
char xmlBuffer[10240];
// xmlBuffer is used to hold the retrieved XML document
integer length;
// Assume a table named dept has been created with the following statement:
// CREATE TABLE dept (id CHAR(8), deptdoc XML)
length = sizeof (xmlBuffer);
SQLExecute (hStmt, "SELECT deptdoc FROM dept WHERE id='001'", SQL_NTS);
SQLBindCol (hStmt, 1, SQL_C_BINARY, xmlBuffer, &length, NULL);
SQLFetch (hStmt);
SQLCloseCursor (hStmt);
// xmlBuffer now contains a valid XML document encoded in UTF-8