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 ShowВ DiffGram.
To view CursorAdapter property settings
-
Click ShowВ CursorAdapterВ Properties.
For more information about CursorAdapter and XMLAdapter, see CursorAdapter Class and XMLAdapter Class.
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.
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
-
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 ThisForm.AddProperty('oXMLAdapter',CREATEOBJECT('XMLAdapter'))
-
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.
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 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:
В | Copy 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:
В | Copy Code |
---|---|
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 |