Condividi tramite


Synchronizing a DataSet with an XmlDataDocument

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 in which you can synchronize a DataSet with an XmlDataDocument. You can:

  • Populate a DataSet with schema (relational structure) and data and then synchronize it with a new XmlDataDocument. This provides a hierarchical view of existing relational data. For example:

    Dim myDataSet As DataSet = New DataSet
    
    ' Add code here to populate the DataSet with schema and data.
    
    Dim xmlDoc As XmlDataDocument = New XmlDataDocument(myDataSet)
    [C#]
    DataSet myDataSet = new DataSet();
    
    // Add Code here to populate the DataSet with schema and data.
    
    XmlDataDocument xmlDoc = new XmlDataDocument(myDataSet);
    
  • 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 will preserve the entire XML document even though the DataSet only exposes a small portion of it. (For a detailed example of this, see Example of Synchronizing a DataSet with an XmlDataDocument.)

    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 myDataSet As DataSet = New DataSet
    
    ' Add code here to populate the DataSet with schema, but not data.
    
    Dim xmlDoc As XmlDataDocument = New XmlDataDocument(myDataSet)
    xmlDoc.Load("XMLDocument.xml")
    [C#]
    DataSet myDataSet = new DataSet();
    
    // Add code here to populate the DataSet with schema, but not data.
    
    XmlDataDocument xmlDoc = new XmlDataDocument(myDataSet);
    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, then access the relational view of the data using the DataSet property of the XmlDataDocument. You will need to set the schema of the DataSet before you will be able to 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 myDataSet As DataSet = xmlDoc.DataSet
    
    ' Add code here to create the schema of the DataSet to view the data.
    
    xmlDoc.Load("XMLDocument.xml")
    [C#]
    XmlDataDocument xmlDoc = new XmlDataDocument();
    DataSet myDataSet = 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 Nested DataRelations.

In This Section

  • XML and the DataSet
    Describes how the DataSet interacts with XML as a data source, including loading and persisting the contents of a DataSet as XML data.
  • Nested DataRelations
    Discusses the importance of nested DataRelation objects when representing the contents of a DataSet as XML data, and describes how to create these relations.
  • Creating and Using DataSets
    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.