Skip to main content
A newer version of this page is available. .

Binding to XML Data

  • 2 minutes to read

The DataSet component allows binding editors to XML data. This topic explains how to do this and provides an example. Please refer to the Data Binding Overview topic for other data binding methods available.

Binding to XML Data

To bind an editor to XML data, you need to create a DataSet object. This object provides methods allowing you to load XML files, streams containing XML data, etc. Once data is loaded into the DataSet, you can bind an editor to a data field using the editor’s DataBindings property.

The image below illustrates binding to XML data.

Xml Binding

When an editor is bound to XML data, changing the editor’s value does not update the data source. Data changes are actually applied to the DataSet where the data was loaded. To apply changes to the data source, use the methods provided by the DataSet class. For instance, if data has been loaded from an XML file using the ReadXML method, you need to call the WriteXML method to overwrite that file.

Note that binding to XML data can only be done at runtime.

Example - Binding an Editor to an XML File

The sample code below binds a text editor control to XML data stored in a file. The editor is bound to a field using the DataBindings property and its Add method. This method requires three arguments: the name of the property to be bound, the data table (data view) containing the desired data and the field name.


// creating a DataSet and filling it with data from an XML file
DataSet xmlDataSet = new DataSet();
xmlDataSet.ReadXml("e:\\DBs\\Cars.xml");

// binding the text editor to the DataSet
textEdit1.DataBindings.Add("EditValue", xmlDataSet.Tables[0], "TradeMark", true);

 

See Also