Quickly Convert Recordset into an XML File

Quickly Convert Recordset into an XML File

With MDAC 2.1 (Microsoft Data Access Components), you can generate a XML file quickly from a Recordset object.

The Save() method of Recordset object takes in an optional parameter, adPersistXML, of type “PersistFormatEnum.” By specifying adPersistXML in the Save() method, you can save the Recordset object into a file in XML format. Similarly, you can reopen a Recordset by providing a previously saved XML file.

Here is an example that saves a Recordset as an XML file:

 Public Sub GenerateXML()   Dim oRecordset As ADODB.Recordset   ' Create Recordset object   Set oRecordset = CreateObject("ADODB.Recordset")   ' Open Recordset   oRecordset.Open "SELECT * FROM MyTable", _      "DSN=Sample;Provider=MSDASQL; uid=sa;pwd=;", _      adOpenDynamic, adLockOptimistic, adCmdText   ' Save Recordset as an XML file   oRecordset.Save "C:MyFolderMyTable.xml", adPersistXML   ' Close Recordset   oRecordset.CloseEnd Sub


Share the Post: