Creating a Cursor from an XML DataSet Sample

File: ...\Samples\Solution\Toledo\CAXML.scx

This sample demonstrates how to create a cursor from an XML DataSet using the CursorAdapter and XMLAdapter classes and how to bind the data to controls on a form.

To see the changes in an XML DiffGram

  • Make changes in the grid and click ShowDiffGram.

To view CursorAdapter property settings

  • Click ShowCursorAdapterProperties.

For more information about CursorAdapter and XMLAdapter, see CursorAdapter Class and XMLAdapter Class.

Loading XML into a Cursor

In this sample, the XMLAdapterLoadXML method loads an XML DataSet into an XMLAdapter object and works with a CursorAdapter object to create a cursor. Loading the XML DataSet into the XMLAdapter creates XMLTable objects in the XMLAdapterTables collection corresponding to the tables in the XML DataSet. The CursorAdapterSelectCmd property is set to the XMLTable object you want to display. When the CursorAdapterCursorFill method is called on the CursorAdapter object, the cursor is created.

For more information, see LoadXML Method, XMLTable Class, and CursorFill Method.

This sample uses an ADO.NET DataSet as the XML file, but the XML file can also be a Visual FoxPro-generated XML DataSet.

To load an XML DataSet into an XMLAdapter object

  1. In the Init event of the sample form, add a property that is an object reference to an XMLAdapter by using the Form or Class designers or by using the AddProperty method in code:

    ThisForm.AddProperty('oXMLAdapter',CREATEOBJECT('XMLAdapter'))
    
  2. To create the Tables collection in the XMLAdapter object, load the XML file as shown in the following code:

    WITH ThisForm.oXMLAdapter
       .LoadXML(ThisForm.cRunPath+'getcustomers.xml',.T.)
    ENDWITH   
    
  3. Set CursorAdapterSelectCmd to the XMLTable object and call the CursorAdapterCursorFill method:

    ThisForm.DataEnvironment.cursor1.SelectCmd= ;
       "ThisForm.oXMLAdapter.Tables.Item(1)"
    ThisForm.DataEnvironment.cursor1.CursorFill()
    

To view the properties of the CursorAdapter object, look in the DataEnvironment object. You can open the DataEnvironment Designer by right-clicking the CursorAdapter object and selecting Builder. The CursorAdapter properties have been set by the CursorAdapter Builder.

For more information, see DataEnvironment Object, Init Event, and CursorAdapter Builder.

Controlling Data Binding

The cursor containing the data that the form uses does not exist when data binding usually occurs. This is because the XMLAdapter object is loading the XML in the form's Init event, which occurs after data binding by default. However, you can control when data binding occurs by setting the FormBindControls property.

In this sample, the form's BindControls property is set to False (.F.). After the CursorAdapterCursorFill method creates the cursor containing the data, BindControls is set to True (.T.). You can then bind the controls on the form using the following code:

ThisForm.BindControls = .T.

For more information, see BindControls Property.

Generating XML DiffGrams with the XMLAdapter

To generate an XML DiffGram, use the XMLAdapter ToXML method, as illustrated in the cmdDiffgram.Click event of this sample:

WITH ThisForm.oXMLAdapter
   .ReleaseXML(.F.)      && Release XML document but preserve schema.
   .UTF8Encoded = .T.    && Indicates International characters.
   .IsDiffgram = .T.     && Generate XML DiffGram.
   llIncludeBefore = .T. && Include <diffgram:before> format.
   llChangesOnly = .T.   && Generate only changes made.
   llIsFile = .F.        && XML is a variable. 
   lcSchemaLocation = "" && Generate inline schema. 
   .ToXml("lcXML",lcSchemaLocation,llIsFile,llIncludeBefore,llChangesOnly) 
ENDWITH

See Also

Solutions Samples | XML Functionality Using XMLAdapters