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 ShowВ DiffGram.
To view CursorAdapter property settings
Click ShowВ CursorAdapterВ Properties.
Loading XML into a Cursor
In this sample, the XMLAdapterВ LoadXML 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 XMLAdapterВ Tables collection corresponding to the tables in the XML DataSet. The CursorAdapterВ SelectCmd property is set to the XMLTable object you want to display. When the CursorAdapterВ CursorFill method is called on the CursorAdapter object, the cursor is created.
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
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:
В Copy Code
To create the Tables collection in the XMLAdapter object, load the XML file as shown in the following code:
В Copy Code
WITH ThisForm.oXMLAdapter .LoadXML(ThisForm.cRunPath+'getcustomers.xml',.T.) ENDWITH
Set CursorAdapterВ SelectCmd to the XMLTable object and call the CursorAdapterВ CursorFill method:
В Copy Code
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.
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 FormВ BindControls property.
In this sample, the form's BindControls property is set to False (.F.). After the CursorAdapterВ CursorFill 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