Here are some ideas and code examples telling how to export a recordset to XML in C++ with CMarkup. If you have a recordset object after running a database SQL query you can save the recordset as XML for logging, migration or later import.

Function to export recordset to XML

This example uses a CVORecordset which is the ADO on Windows CE (ADOCE) recordset for the Windows mobile platform. But most recordset classes for C++ have similar methods.

void ExportRecordsetToXML( CMarkup& xml, CVORecordset* pRecordset )
{
  // Create a table document (or subdocument) in xml
  xml.AddElem( "table" );
  xml.IntoElem();
  pRecordset->MoveFirst();
  int nFieldCount = pRecordset->GetFieldCount();
  while ( ! pRecordset->IsEOF() )
  {
    // Create a record element for every record in the recordset
    xml.AddElem( "record" );
    for ( int nField = 0; nField < nFieldCount; ++nField )
    {
      // Get the value as a string and export with SetAttrib or AddChildElem
      CString strValue;
      pRecordset->GetFieldValue( nRecord, strValue );    
      xml.SetAttrib( pRecordset->GetFieldName(nField), strValue );
      // xml.AddChildElem( pRecordset->GetFieldName(nField), strValue );
    }
    pRecordset->MoveNext();
  }
  xml.OutOfElem();
}

If you want to put each value in an element rather than an attribute, use the AddChildElem function (shown in the remark) instead of the SetAttrib method.

How to save recordset to XML file

CMarkup xml;
ExportRecordsetToXML( xml, &recordset );
xml.SetAttrib( "name", "users" )
xml.Save( "users.xml" );

ExportRecordsetToXML returns with the table element at the main position so you can set the table name as an attribute of the table element:

<table name="users">
  <record username="john" userid="2"/>
  <record username="jane" userid="9"/>
</table>

Export XML to HTML table

Or if you want an HTML format.

<table>
<tr><td>john</td><td>2</td></tr>
<tr><td>jane</td><td>9</td></tr>
</table>

The following code to export a record keeps a row on one HTML line using the MNF_WITHNOLINES flag.

    xml.AddElem( "tr" );
    for ( int nField = 0; nField < nFieldCount; ++nField )
    {
      CString strValue;
      pRecordset->GetFieldValue( nRecord, strValue );    
      xml.AddChildElem( "td", strValue, MNF_WITHNOLINES );
    }

How to generically export table to XML

The following function demonstrates how to generically export a table to XML file by just selecting all fields and rows from the given table name.

bool ExportTableToXML( CMarkup& xml, CString strTableName )
{
  CVORecordset recordset( theApp.m_Connection );
  CString strQuery = "SELECT * from " + strTableName;
  recordset.Open( strQuery, adOpenForwardOnly, adLockReadOnly );
  if ( recordset.IsOpen() )
  {
    ExportRecordsetToXML( xml, &recordset );
    xml.SetAttrib( "name", strTableName );
  }
  recordset.Close();
}

Save multiple recordsets in an XML file

The ExportTableToXML function can be used to save multiple recordsets in an XML file.

CMarkup xml;
ExportTableToXML( xml, "users" );
ExportTableToXML( xml, "events" );
xml.Save( "tables.xml" );

See also:

XML Serialization in C++ with CMarkup