Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Obtain an ADO Recordset from XML


View products that this article applies to.

This article was previously published under Q263247

↑ Back to the top


Summary

If you have an XML string or document you may use it to open an ActiveX Data Objects (ADO) recordset. You can do this by using the Microsoft ADO 2.5 Stream object and the new XML integration features in ADO.

This article describes the mechanism required to load the following into an ADO 2.5 recordset:
  • A string containing XML.

    -and-

  • An XML DOM Document instance.
Provided the XML data is in the format where ADO recordset can accept.

↑ Back to the top


More information

To open an XML string as a recordset, use the code that follows. Make sure that your Visual Basic project has the appropriate references to the Microsoft ActiveX Data Access Components 2.5 Library so that ADO can be used as shown.

Sample Code
Public Function RecordsetFromXMLString(sXML As String) As Recordset

    Dim oStream As ADODB.Stream
    Set oStream = New ADODB.Stream
    
    oStream.Open
    oStream.WriteText sXML   'Give the XML string to the ADO Stream

    oStream.Position = 0    'Set the stream position to the start

    Dim oRecordset As ADODB.Recordset
    Set oRecordset = New ADODB.Recordset
       
    oRecordset.Open oStream    'Open a recordset from the stream

    oStream.Close
    Set oStream = Nothing

    Set RecordsetFromXMLString = oRecordset  'Return the recordset

    Set oRecordset = Nothing

End Function
Please note that the preceding code accepts a String.


The following code accepts a DOM object as the source for the recordset. Note that the Stream object is not needed:

Public Function RecordsetFromXMLDocument(XMLDOMDocument As DOMDocument) As Recordset
    Dim oRecordset As ADODB.Recordset
    
    Set oRecordset = New ADODB.Recordset
       
    oRecordset.Open XMLDOMDocument 'pass the DOM Document instance as the Source argument

    Set RecordsetFromXMLDocument = oRecordset  'return the recordset

    Set oRecordset = Nothing

End Function
After you open the recordset in any of these ways, you can use the recordset as any other disconnected recordset.

Note that the current position, AbsolutePage and other navigation properties are not stored in the XML document; therefore, the newly opened recordset is always positioned at the first row.

↑ Back to the top


References

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
309702� How To Read XML Data into a DataSet by Using Visual Basic .NET
For more information about ADO, see to the following Web site: (c) Microsoft Corporation 2000, All Rights Reserved. Contributions by Edward A. Jezierski, Microsoft Corporation.

↑ Back to the top


Keywords: KB263247, kbhowto, kbcodesnippet

↑ Back to the top

Article Info
Article ID : 263247
Revision : 9
Created on : 7/1/2004
Published on : 7/1/2004
Exists online : False
Views : 482