数据集和 XmlDataDocument 同步DataSet and XmlDataDocument Synchronization

ADO.NET DataSet 为您提供了数据的关系表示形式。The ADO.NET DataSet provides you with a relational representation of data. 若要实现分层数据访问,可以使用 .NET Framework 中的可用 XML 类。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. 不过,.NET Framework 通过分别通过DataSet对象和XmlDataDocument对象实现对数据的关系和分层表示形式的实时同步访问。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.

如果数据集XmlDataDocument同步,则这两个对象使用单个数据集。When a DataSet is synchronized with an XmlDataDocument, both objects are working with a single set of data. 这意味着,如果对数据集进行了更改,则所做的更改将反映在XmlDataDocument中,反之亦然。This means that if a change is made to the DataSet, the change will be reflected in the XmlDataDocument, and vice versa. 数据集XmlDataDocument之间的关系通过允许单个应用程序(使用单个数据集)访问围绕数据集构建的整个服务套件(如 Web 窗体和Windows 窗体控件和 Visual Studio .NET 设计器)以及 XML 服务套件,包括可扩展样式表语言(XSL)、XSL 转换(XSLT)和 XML 路径语言(XPath)。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.

可以通过多种方式将数据集XmlDataDocument同步。There are several ways that you can synchronize a DataSet with an XmlDataDocument. 你可以:You can:

  • 使用架构(即关系结构)和数据填充数据集,然后将其与新的XmlDataDocument同步。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);  
    
  • 仅使用架构填充数据集(如强类型化数据集),将其与XMLDATADOCUMENT同步,然后从 XML 文档加载XmlDataDocumentPopulate 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. 数据集架构中的表名和列名必须与要与其同步的 XML 元素的名称相匹配。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.

    请注意,该数据集的架构只需要匹配您要在关系视图中公开的 XML 元素。Note that the schema of the DataSet only needs to match the XML elements that you want to expose in your relational view. 这样,就可以具有非常大的 XML 文档,而该文档上可以有非常小的关系“窗口”。This way, you can have a very large XML document and a very small relational "window" on that document. 即使数据集只公开其中的一小部分, XmlDataDocument仍保留整个 XML 文档。The XmlDataDocument preserves the entire XML document even though the DataSet only exposes a small portion of it. (有关此示例的详细示例,请参阅使用 XmlDataDocument 同步数据集。)(For a detailed example of this, see Synchronizing a DataSet with an XmlDataDocument.)

    下面的代码示例演示了创建数据集并填充其架构,然后将其与XmlDataDocument同步的步骤。The following code example shows the steps for creating a DataSet and populating its schema, then synchronizing it with an XmlDataDocument. 请注意, DataSet架构只需要与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");  
    

    如果XmlDataDocument与包含数据的数据集同步,则无法加载它。You cannot load an XmlDataDocument if it is synchronized with a DataSet that contains data. 否则会引发异常。An exception will be thrown.

  • 创建新的XmlDataDocument并从 XML 文档加载它,然后使用XmlDataDocumentDataSet属性访问数据的关系视图。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. 你需要设置数据集的架构,然后才能使用dataset查看XmlDataDocument中的任何数据。You need to set the schema of the DataSet before you can view any of the data in the XmlDataDocument using the DataSet. 同样,数据集架构中的表名和列名必须与要与其同步的 XML 元素的名称相匹配。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.

    下面的代码示例演示如何访问XmlDataDocument中数据的关系视图。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");  
    

同步XmlDataDocument数据集的另一个优点是,XML 文档的保真度得以保留。Another advantage of synchronizing an XmlDataDocument with a DataSet is that the fidelity of an XML document is preserved. 如果使用ReadXml从 xml 文档填充数据,则当使用WRITEXML将数据写回 xml 文档时,它可能会与原始 XML 文档有很大差异。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. 这是因为数据集不会保留 XML 文档中的格式设置,如空白或层次结构信息(例如元素顺序)。This is because the DataSet does not maintain formatting, such as white space, or hierarchical information, such as element order, from the XML document. 数据集还不包含 XML 文档中的元素,因为这些元素与DataSet的架构不匹配。The DataSet also does not contain elements from the XML document that were ignored because they did not match the schema of the Dataset. XmlDataDocument数据集同步,可以在XmlDataDocument中维护原始 XML 文档的格式设置和分层元素结构,而数据集只包含数据,适用于数据集的架构信息。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.

数据集XmlDataDocument同步时,结果可能会有所不同,具体取决于是否DataRelation嵌套了您的对象。When synchronizing a DataSet with an XmlDataDocument, results may differ depending on whether or not your DataRelation objects are nested. 有关详细信息,请参阅嵌套 datarelationFor more information, see Nesting DataRelations.

本节内容In This Section

将数据集与 XmlDataDocument 同步Synchronizing a DataSet with an XmlDataDocument
演示如何使用XmlDataDocument将强类型化数据集与最小架构同步。Demonstrates synchronizing a strongly typed DataSet, with minimal schema, with an XmlDataDocument.

对数据集执行 XPath 查询Performing an XPath Query on a DataSet
演示如何对数据集的内容执行 XPath 查询。Demonstrates performing an XPath query on the contents of a DataSet.

将 XSLT 转换应用于 DataSetApplying an XSLT Transform to a DataSet
演示如何将 XSLT 转换应用于数据集的内容。Demonstrates applying an XSLT transform to the contents of a DataSet.

在数据集中使用 XMLUsing XML in a DataSet
介绍如何将数据与 xml 作为数据源进行交互,包括将数据集的内容作为 XML 数据进行加载和保存。Describes how the DataSet interacts with XML as a data source, including loading and persisting the contents of a DataSet as XML data.

嵌套 DataRelationNesting DataRelations
讨论嵌套的DataRelation对象在以 XML 数据形式表示数据集的内容时的重要性,并描述如何创建这些关系。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
介绍数据集,以及如何使用它来管理应用程序数据以及与包括关系数据库和 XML 在内的数据源进行交互。Describes the DataSet and how to use it to manage application data and to interact with data sources including relational databases and XML.

XmlDataDocument
包含有关XmlDataDocument类的参考信息。Contains reference information about the XmlDataDocument class.

请参阅See also