Microsoft KB Archive/271619

= How To Retrieve XML Data by Using an XPath Query in a Visual Basic Client =

Article ID: 271619

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft SQL Server 2000 Standard Edition
 * Microsoft Data Access Components 2.6
 * Microsoft Data Access Components 2.7
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft XML Parser 2.6
 * Microsoft XML Core Services 4.0
 * Microsoft XML Core Services 4.0

-



This article was previously published under Q271619





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



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.



MORE INFORMATION
 Create a new Visual Basic application. Form1 is created by default. On the Project menu, click References, and then set a reference to Microsoft ActiveX Data Objects. 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. Create two CommandButton controls, that are named and labeled cmdTestIt, and cmdExitProgram, respectively.  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 = &quot;{C8B521FB-5CF3-11CE-ADE5-00AA0044773D}&quot; Const DBGUID_SQL As String = &quot;{C8B522D7-5CF3-11CE-ADE5-00AA0044773D}&quot; Const DBGUID_MSSQLXML As String = &quot;{5D531CB2-E6Ed-11D2-B252-00C04F681B71}&quot; Const DBGUID_XPATH As String = &quot;{ec2a4293-e898-11d2-b1b7-00c04f680c56}&quot;

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(&quot;Mapping Schema&quot;) = App.Path & &quot;\CustomerOrder.xdr&quot; '  hook up the command to the result stream cmd.Properties(&quot;Output Stream&quot;) = strm '  trim off any additional space

txtXPath = Trim(txtXPath) If txtXPath = &quot;&quot; Then '  no search path default to customers. (CasE SeNsiTiVe).... txtXPath = &quot;Customers&quot; 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, &quot;><&quot;, &quot;>&quot; & vbCrLf & &quot;<&quot;)

'  reset the stream's position in order to read it    strm.Position = 0

strm.Close

GoTo cleanup

trap:

' report errors MsgBox &quot;Error (&quot; & Err.Number & &quot;) -- &quot; & 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 = &quot;PROVIDER=SQLOLEDB;Data Source=.;Initial Catalog=Northwind;uid=sa;pwd=&quot; gCn.Open Exit Sub

trap: MsgBox &quot;Failed to connect to database. Program Shutting down.&quot; Unload Me   End

End Sub   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=&quot;1.0&quot; ?>

<Schema xmlns=&quot;urn:schemas-microsoft-com:xml-data&quot; xmlns:dt=&quot;urn:schemas-microsoft-com:datatypes&quot; xmlns:sql=&quot;urn:schemas-microsoft-com:xml-sql&quot;>

<ElementType name=&quot;CustOrder&quot; sql:relation=&quot;Orders&quot;> <AttributeType name = &quot;CustomerID&quot; /> <AttributeType name = &quot;OrderID&quot; /> <AttributeType name = &quot;OrderDate&quot; /> <AttributeType name = &quot;ShipCity&quot; />

<attribute type = &quot;CustomerID&quot; sql:field=&quot;CustomerID&quot; /> <attribute type = &quot;OrderID&quot;    sql:field=&quot;OrderID&quot; /> <attribute type = &quot;OrderDate&quot;  sql:field=&quot;OrderDate&quot; /> <attribute type = &quot;ShipCity&quot;   sql:field=&quot;ShipCity&quot; />

</ElementType>

<ElementType name=&quot;Customers&quot; sql:relation=&quot;Customers&quot;>

<AttributeType name = &quot;CustomerID&quot; /> <AttributeType name = &quot;CompanyName&quot; /> <AttributeType name = &quot;ContactName&quot; /> <attribute type = &quot;CustomerID&quot;  sql:field=&quot;CustomerID&quot; /> <attribute type = &quot;CompanyName&quot; sql:field=&quot;CompanyName&quot; /> <attribute type = &quot;ContactName&quot; sql:field=&quot;ContactName&quot; /> <element type=&quot;CustOrder&quot; > <sql:relationship key-relation=&quot;Customers&quot; key=&quot;CustomerID&quot; foreign-key=&quot;CustomerID&quot; foreign-relation=&quot;Orders&quot; /> </ElementType> </Schema> </li>  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'] </li></ol>

<div class="references_section">