DataSet and XmlDataDocument Synchronization (ADO.NET)
The ADO.NET DataSet provides you with a relational representation of data. For hierarchical data access, you can use the XML classes available in the .NET Framework. Historically, these two representations of data have been used separately. However, the .NET Framework enables real-time, synchronous access to both the relational and hierarchical representations of data through the DataSet object and the XmlDataDocument object, respectively.
When a DataSet is synchronized with an XmlDataDocument, both objects are working with a single set of data. This means that if a change is made to the DataSet, the change will be reflected in the XmlDataDocument, and vice versa. The relationship between the DataSet and the XmlDataDocument creates great flexibility by allowing a single application, using a single set of data, to access the entire suite of services built around the DataSet (such as Web Forms and Windows Forms controls, and Visual Studio .NET designers), as well as the suite of XML services including Extensible Stylesheet Language (XSL), XSL Transformations (XSLT), and XML Path Language (XPath). You do not have to choose which set of services to target with the application; both are available.
There are several ways that you can synchronize a DataSet with an XmlDataDocument. You can:
Populate a DataSet with schema (that is, a relational structure) and data and then synchronize it with a new XmlDataDocument. This provides a hierarchical view of existing relational data. For example:
Dim dataSet As DataSet = New DataSet ' Add code here to populate the DataSet with schema and data. Dim xmlDoc As XmlDataDocument = New XmlDataDocument(dataSet)
DataSet dataSet = new DataSet(); // Add code here to populate the DataSet with schema and data. XmlDataDocument xmlDoc = new XmlDataDocument(dataSet);
Populate a DataSet with schema only (such as a strongly typed DataSet), synchronize it with an XmlDataDocument, and then load the XmlDataDocument from an XML document. This provides a relational view of existing hierarchical data. The table names and column names in your DataSet schema must match the names of the XML elements that you want them synchronized with. This matching is case-sensitive.
Note that the schema of the DataSet only needs to match the XML elements that you want to expose in your relational view. This way, you can have a very large XML document and a very small relational "window" on that document. The XmlDataDocument preserves the entire XML document even though the DataSet only exposes a small portion of it. (For a detailed example of this, see Synchronizing a DataSet with an XmlDataDocument (ADO.NET).)
The following code example shows the steps for creating a DataSet and populating its schema, then synchronizing it with an XmlDataDocument. Note that the DataSet schema only needs to match the elements from the XmlDataDocument that you want to expose using the DataSet.
Dim dataSet As DataSet = New DataSet ' Add code here to populate the DataSet with schema, but not data. Dim xmlDoc As XmlDataDocument = New XmlDataDocument(dataSet) xmlDoc.Load("XMLDocument.xml")
DataSet dataSet = new DataSet(); // Add code here to populate the DataSet with schema, but not data. XmlDataDocument xmlDoc = new XmlDataDocument(dataSet); xmlDoc.Load("XMLDocument.xml");
You cannot load an XmlDataDocument if it is synchronized with a DataSet that contains data. An exception will be thrown.
Create a new XmlDataDocument and load it from an XML document, and then access the relational view of the data using the DataSet property of the XmlDataDocument. You need to set the schema of the DataSet before you can view any of the data in the XmlDataDocument using the DataSet. Again, the table names and column names in your DataSet schema must match the names of the XML elements that you want them synchronized with. This matching is case-sensitive.
The following code example shows how to access the relational view of the data in an XmlDataDocument.
Dim xmlDoc As XmlDataDocument = New XmlDataDocument Dim dataSet As DataSet = xmlDoc.DataSet ' Add code here to create the schema of the DataSet to view the data. xmlDoc.Load("XMLDocument.xml")
XmlDataDocument xmlDoc = new XmlDataDocument(); DataSet dataSet = xmlDoc.DataSet; // Add code here to create the schema of the DataSet to view the data. xmlDoc.Load("XMLDocument.xml");
Another advantage of synchronizing an XmlDataDocument with a DataSet is that the fidelity of an XML document is preserved. If the DataSet is populated from an XML document using ReadXml, when the data is written back as an XML document using WriteXml it may differ dramatically from the original XML document. This is because the DataSet does not maintain formatting, such as white space, or hierarchical information, such as element order, from the XML document. The DataSet also does not contain elements from the XML document that were ignored because they did not match the schema of the Dataset. Synchronizing an XmlDataDocument with a DataSet allows the formatting and hierarchical element structure of the original XML document to be maintained in the XmlDataDocument, while the DataSet contains only data and schema information appropriate to the DataSet.
When synchronizing a DataSet with an XmlDataDocument, results may differ depending on whether or not your DataRelation objects are nested. For more information, see Nesting DataRelations (ADO.NET).
In This Section
Synchronizing a DataSet with an XmlDataDocument (ADO.NET)
Demonstrates synchronizing a strongly typed DataSet, with minimal schema, with an XmlDataDocument.Performing an XPath Query on a DataSet (ADO.NET)
Demonstrates performing an XPath query on the contents of a DataSet.Appling an XSLT Transform to a DataSet (ADO.NET)
Demonstrates applying an XSLT transform to the contents of a DataSet.
Related Sections
Using XML in a DataSet (ADO.NET)
Describes how the DataSet interacts with XML as a data source, including loading and persisting the contents of a DataSet as XML data.Nesting DataRelations (ADO.NET)
Discusses the importance of nested DataRelation objects when representing the contents of a DataSet as XML data, and describes how to create these relations.DataSets, DataTables, and DataViews (ADO.NET)
Describes the DataSet and how to use it to manage application data and to interact with data sources including relational databases and XML.XmlDataDocument Class
Contains reference information about the XmlDataDocument class.