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 Retrieve XML Data by Using an XPath Query in a Visual Basic Client


View products that this article applies to.

For a Microsoft Visual Basic .NET version of this article, see 301111.

↑ Back to the top


Summary

This article demonstrates how to programmatically retrieve an XML data stream from a SQL Server 2000 mapping schema by using an XPath query. The scope operates in a client/server (2-tier) model.

This sample also allows you to test XPath queries against the mapping schema. For each query, the text box of the form displays the XML; the elements of the XML data stream are broken onto separate lines for viewing purposes.

↑ Back to the top


More information

  1. Create a new Visual Basic application. Form1 is created by default.
  2. On the Project menu, click References, and then set a reference to Microsoft ActiveX Data Objects.
  3. Create two TextBox controls, that ae named and labeled txtResults and txtXPath, respectively. Size txtXPath to the width for your form and one line in height. To display the results, size txtResults as large as possible, and then set the Multi-line property to True.
  4. Create two CommandButton controls, that are named and labeled cmdTestIt, and cmdExitProgram, respectively.
  5. On the code window of Form1, paste the following code:
    Option Explicit
    
    Dim gCn As New ADODB.Connection
    
    '
    ' DBGUID values for the command object's dialect property
    '
    Const DBGUID_DEFAULT As String = "{C8B521FB-5CF3-11CE-ADE5-00AA0044773D}"
    Const DBGUID_SQL As String = "{C8B522D7-5CF3-11CE-ADE5-00AA0044773D}"
    Const DBGUID_MSSQLXML As String = "{5D531CB2-E6Ed-11D2-B252-00C04F681B71}"
    Const DBGUID_XPATH As String = "{ec2a4293-e898-11d2-b1b7-00c04f680c56}"
    
    Private Sub cmdExitProgram_Click()
        Unload Me
        End
    End Sub
    
    Private Sub cmdTestIt_Click()
    
        '   ADODB.Command used for the query
        Dim cmd As ADODB.Command
        '   ADODB.Stream used to set up the command to execute
        Dim strm As ADODB.Stream
    
        On Error GoTo trap
        '   create a new ADODB.Command
        Set cmd = New ADODB.Command
        '   establish connect for the command object to the database
        Set cmd.ActiveConnection = gCn
    
        '   create the ADODB.Stream for the results.
        Set strm = New ADODB.Stream
        '   open the result stream so it may receive the output from the execute
        strm.Open
        '   set the command type to an XPath query
        cmd.Dialect = DBGUID_XPATH
    
        '   set the file name for the mapping schema
        cmd.Properties("Mapping Schema") = App.Path & "\CustomerOrder.xdr"
        '   hook up the command to the result stream
        cmd.Properties("Output Stream") = strm
        '   trim off any additional space 
    
        txtXPath = Trim(txtXPath)
        If txtXPath = "" Then
            '   no search path default to customers. (CasE SeNsiTiVe)....
            txtXPath = "Customers"
        End If
    
        '   set the actual text for the XPath command
        cmd.CommandText = txtXPath
    
        '   execute the command stream
        cmd.Execute , , adExecuteStream
    
        '   reset the stream's position in order to read it 
        strm.Position = 0
    
        '   set the displayed results to the command's output
        txtResults = strm.ReadText
    
        '   clean up the output to make easier to read 
        txtResults = Replace(txtResults, "><", ">" & vbCrLf & "<")
    
       '   reset the stream's position in order to read it
        strm.Position = 0
    
        strm.Close
    
    GoTo cleanup
    
    trap:
    
        ' report errors 
        MsgBox "Error (" & Err.Number & ") -- " & Err.Description
    
    cleanup:
        '   clean up 
        Set strm = Nothing
        Set cmd = Nothing
    
        Exit Sub
    
    End Sub
    
    Private Sub Form_Load()
    
    On Error GoTo trap
        Set gCn = New ADODB.Connection
        gCn.ConnectionString = "PROVIDER=SQLOLEDB;Data Source=.;Initial Catalog=Northwind;uid=sa;pwd="
        gCn.Open
        Exit Sub
    
    trap:
        MsgBox "Failed to connect to database.  Program Shutting down."
        Unload Me
        End
    
    End Sub
    					
  6. Create the mapping schema against which the XPath queries. Either place the file into the project folder (app.path), or fully qualify the path to the file where the Mapping Schema property is set. Save as CustomerOrder.xdr.
    <?xml version="1.0" ?>
    
    <Schema xmlns="urn:schemas-microsoft-com:xml-data"
            xmlns:dt="urn:schemas-microsoft-com:datatypes"
            xmlns:sql="urn:schemas-microsoft-com:xml-sql">
    
        <ElementType name="CustOrder" sql:relation="Orders">
            <AttributeType name = "CustomerID" />
            <AttributeType name = "OrderID" />
            <AttributeType name = "OrderDate" />
            <AttributeType name = "ShipCity" />
    
            <attribute type = "CustomerID"  sql:field="CustomerID" />
            <attribute type = "OrderID"     sql:field="OrderID" />
            <attribute type = "OrderDate"   sql:field="OrderDate" />
            <attribute type = "ShipCity"    sql:field="ShipCity" />
    
        </ElementType>
    
        <ElementType name="Customers" sql:relation="Customers">
    
            <AttributeType name = "CustomerID" />
            <AttributeType name = "CompanyName" />
            <AttributeType name = "ContactName" />
            
            <attribute type = "CustomerID"   sql:field="CustomerID" />
            <attribute type = "CompanyName"  sql:field="CompanyName" />
            <attribute type = "ContactName"  sql:field="ContactName" />
            
            <element type="CustOrder" >
                <sql:relationship key-relation="Customers"
                               key="CustomerID"
                               foreign-key="CustomerID"
                               foreign-relation="Orders"  />
            </element>
            
        </ElementType>
        
    </Schema>
    					
  7. Save the mapping schema and run the sample. To test the query, you can enter the following XPath queries into the txtXPath textbox. (The default is set to Customers.)
        Customers
        Customers[@CustomerID]
        Customers[@CustomerID='ALFKI']
        Customers/CustOrder[@CustomerID='ALFKI']
    					

↑ Back to the top


References

For SQL Server 2000, please see SQL Server Books Online.

For XML, see the MSDN page on the following Microsoft Web site:

↑ Back to the top


Keywords: KB271619, kbhowto

↑ Back to the top

Article Info
Article ID : 271619
Revision : 10
Created on : 7/13/2004
Published on : 7/13/2004
Exists online : False
Views : 596