从 XML 加载数据集Loading a DataSet from XML

ADO.NET DataSet 的内容可以从 XML 流或文档创建。The contents of an ADO.NET DataSet can be created from an XML stream or document. 此外,利用 .NET Framework,您可以相当灵活地控制从 XML 中加载哪些信息以及如何创建 DataSet 的架构(即关系结构)。In addition, with the .NET Framework you have great flexibility over what information is loaded from XML, and how the schema or relational structure of the DataSet is created.

若要使用 DataSet XML 中的数据填充,请使用对象的 ReadXml 方法 DataSetTo fill a DataSet with data from XML, use the ReadXml method of the DataSet object. ReadXml方法从文件、流或XmlReader中进行读取,并将 XML 的源以及可选的XmlReadMode参数作为参数。The ReadXml method reads from a file, a stream, or an XmlReader, and takes as arguments the source of the XML plus an optional XmlReadMode argument. 有关 XmlReader的详细信息,请参阅 用 XmlTextReader 读取 XML 数据For more information about the XmlReader, see Reading XML Data with XmlTextReader. ReadXml方法读取 XML 流或文档的内容,并加载 DataSet 带有数据的。The ReadXml method reads the contents of the XML stream or document and loads the DataSet with data. 它还会根据指定的 XmlReadMode 创建的关系架构 DataSetXmlReadMode以及关系架构是否已存在。It will also create the relational schema of the DataSet depending on the XmlReadMode specified and whether or not a relational schema already exists.

下表描述了 XmlReadMode 参数的选项。The following table describes the options for the XmlReadMode argument.

选项Option 说明Description
AutoAuto 这是默认设置。This is the default. 检查 XML 并按如下顺序选择最适合的选项:Examines the XML and chooses the most appropriate option in the following order:

-如果 XML 为 DiffGram,则使用 diffgram- If the XML is a DiffGram, DiffGram is used.
-如果 DataSet 包含架构或 XML 包含内联架构,则使用 ReadSchema- If the DataSet contains a schema or the XML contains an inline schema, ReadSchema is used.
-如果不 DataSet 包含架构并且 XML 不包含内联架构,则使用 InferSchema- If the DataSet does not contain a schema and the XML does not contain an inline schema, InferSchema is used.

如果知道要读取的 XML 的格式,则为了获得最佳性能,建议设置显式 XmlReadMode,而不是接受 自动 默认值。If you know the format of the XML being read, for best performance it is recommended that you set an explicit XmlReadMode, rather than accept the Auto default.
ReadSchemaReadSchema 读取内联架构并加载数据和架构。Reads any inline schema and loads the data and schema.

如果 DataSet 已包含架构,则新表将从内联架构添加到 DataSet 中的现有架构。If the DataSet already contains a schema, new tables are added from the inline schema to the existing schema in the DataSet. 如果 DataSet 中已存在内联架构中的任何表,则将引发异常。If any tables in the inline schema already exist in the DataSet, an exception is thrown. 您将不能使用 XmlReadMode修改现有表的架构。You will not be able to modify the schema of an existing table using XmlReadMode.ReadSchema.

如果 DataSet 不包含架构,并且没有内联架构,则不会读取任何数据。If the DataSet does not contain a schema, and there is no inline schema, no data is read.

内联架构可以使用 XML 架构定义语言 (XSD) 架构来定义。Inline schema can be defined using XML Schema definition language (XSD) schema. 有关将内联架构作为 XML 架构写入的详细信息,请参阅 从 Xml 架构派生数据集关系结构 (XSD) For details about writing inline schema as XML Schema, see Deriving DataSet Relational Structure from XML Schema (XSD).
IgnoreSchemaIgnoreSchema 忽略任何内联架构并将数据加载到现有的 DataSet 架构中。Ignores any inline schema and loads the data into the existing DataSet schema. 任何与现有架构不匹配的数据都将被丢弃。Any data that does not match the existing schema is discarded. 如果 DataSet 中不存在任何架构,则不会加载任何数据。If no schema exists in the DataSet, no data is loaded.

如果数据为 DiffGram,则IgnoreSchema具有与 diffgram 相同的功能DiffGram If the data is a DiffGram, IgnoreSchema has the same functionality as DiffGram .
InferSchemaInferSchema 忽略任何内联架构并按照 XML 数据的结构推断架构,然后加载数据。Ignores any inline schema and infers the schema per the structure of the XML data, then loads the data.

如果 DataSet 已经包含架构,则会通过向现有表中添加列来扩展当前架构。If the DataSet already contains a schema, the current schema is extended by adding columns to existing tables. 如果不存在表,则不会添加其他表。Extra tables will not be added if there are not existing tables. 如果已存在具有不同命名空间的推断表或者任何推断表与现有列发生冲突,则会引发异常。An exception is thrown if an inferred table already exists with a different namespace, or if any inferred columns conflict with existing columns.

有关 ReadXmlSchema 如何从 xml 文档推断架构的详细信息,请参阅 从 Xml 推断数据集关系结构For details about how ReadXmlSchema infers a schema from an XML document, see Inferring DataSet Relational Structure from XML.
DiffGramDiffGram 读取 DiffGram 并将数据添加到当前架构中。Reads a DiffGram and adds the data to the current schema. DiffGram 合并具有唯一标识符值匹配的现有行的新行。DiffGram merges new rows with existing rows where the unique identifier values match. 请参见本主题末尾的“合并 XML 中的数据”。See "Merging Data from XML" at the end of this topic. 有关 Diffgram 的详细信息,请参阅 diffgramFor more information about DiffGrams, see DiffGrams.
FragmentFragment 持续读取多个 XML 片断,直至到达流的末尾。Continues reading multiple XML fragments until the end of the stream is reached. DataSet 架构匹配的片断会追加到相应的表。Fragments that match the DataSet schema are appended to the appropriate tables. DataSet 架构不匹配的片断将被丢弃。Fragments that do not match the DataSet schema are discarded.

备注

如果将 XmlReader 传递给 ReadXml ,并且将其定位到 XML 文档中,则 ReadXml 将读取到下一个元素节点,并将其视为根元素,一直读取到元素节点的末尾。If you pass an XmlReader to ReadXml that is positioned part of the way into an XML document, ReadXml will read to the next element node and will treat that as the root element, reading until the end of the element node only. 如果指定 XmlReadMode,则此功能不适用。This does not apply if you specify XmlReadMode.Fragment.

DTD 实体DTD Entities

如果 XML 包含在文档类型定义 (DTD) 架构中定义的实体,则当您尝试 DataSet 通过将文件名、流或非验证 XmlReader 传递到 ReadXml来加载时,将引发异常。If your XML contains entities defined in a document type definition (DTD) schema, an exception will be thrown if you attempt to load a DataSet by passing a file name, stream, or non-validating XmlReader to ReadXml. 相反,必须创建 XmlValidatingReader,并将 EntityHandling 设置为 entityhandling.expandentities,并将 XmlValidatingReader 传递到 ReadXmlInstead, you must create an XmlValidatingReader, with EntityHandling set to EntityHandling.ExpandEntities, and pass your XmlValidatingReader to ReadXml. 在读取实体之前, XmlValidatingReader 会将其展开 DataSetThe XmlValidatingReader will expand the entities prior to being read by the DataSet.

以下代码示例显示如何从 XML 流中加载 DataSetThe following code examples show how to load a DataSet from an XML stream. 第一个示例显示了传递给 ReadXml 方法的文件名。The first example shows a file name being passed to the ReadXml method. 第二个示例显示一个字符串,包含使用 StringReader 加载的 XML。The second example shows a string that contains XML being loaded using a StringReader.

Dim dataSet As DataSet = New DataSet  
dataSet.ReadXml("input.xml", XmlReadMode.ReadSchema)  
DataSet dataSet = new DataSet();  
dataSet.ReadXml("input.xml", XmlReadMode.ReadSchema);  
Dim dataSet As DataSet = New DataSet  
Dim dataTable As DataTable = New DataTable("table1")  
dataTable.Columns.Add("col1", Type.GetType("System.String"))  
dataSet.Tables.Add(dataTable)  
  
Dim xmlData As String = "<XmlDS><table1><col1>Value1</col1></table1><table1><col1>Value2</col1></table1></XmlDS>"  
  
Dim xmlSR As System.IO.StringReader = New System.IO.StringReader(xmlData)  
  
dataSet.ReadXml(xmlSR, XmlReadMode.IgnoreSchema)  
DataSet dataSet = new DataSet();  
DataTable dataTable = new DataTable("table1");  
dataTable.Columns.Add("col1", typeof(string));  
dataSet.Tables.Add(dataTable);  
  
string xmlData = "<XmlDS><table1><col1>Value1</col1></table1><table1><col1>Value2</col1></table1></XmlDS>";  
  
System.IO.StringReader xmlSR = new System.IO.StringReader(xmlData);  
  
dataSet.ReadXml(xmlSR, XmlReadMode.IgnoreSchema);  

备注

如果调用 ReadXml 来加载非常大的文件,则可能会遇到性能下降的情况。If you call ReadXml to load a very large file, you may encounter slow performance. 若要确保 ReadXml的最佳性能,请在大文件上 BeginLoadData 为中的每个表调用方法, DataSet 然后调用 ReadXmlTo ensure best performance for ReadXml, on a large file, call the BeginLoadData method for each table in the DataSet, and then call ReadXml. 最后,为 EndLoadData 中的每个表调用 DataSet,如下面的示例所示。Finally, call EndLoadData for each table in the DataSet, as shown in the following example.

Dim dataTable As DataTable  
  
For Each dataTable In dataSet.Tables  
   dataTable.BeginLoadData()  
Next  
  
dataSet.ReadXml("file.xml")  
  
For Each dataTable in dataSet.Tables  
   dataTable.EndLoadData()  
Next  
foreach (DataTable dataTable in dataSet.Tables)  
   dataTable.BeginLoadData();  
  
dataSet.ReadXml("file.xml");
  
foreach (DataTable dataTable in dataSet.Tables)  
   dataTable.EndLoadData();  

备注

如果的 XSD 架构 DataSet 包含 targetNamespace,则数据可能不会被读取,并且你可能会在调用 ReadXml 以加载 DataSet 包含无限定命名空间的元素的 XML 时遇到异常。If the XSD schema for your DataSet includes a targetNamespace, data may not be read, and you may encounter exceptions, when calling ReadXml to load the DataSet with XML that contains elements with no qualifying namespace. 若要在这种情况下读取未限定的元素,请在 XSD 架构中将 elementFormDefault 设置为等于 "合格的"。To read unqualified elements in this case, set elementFormDefault equal to "qualified" in your XSD schema. 例如:For example:

<xsd:schema id="customDataSet"
  elementFormDefault="qualified"  
  targetNamespace="http://www.tempuri.org/customDataSet.xsd"
  xmlns="http://www.tempuri.org/customDataSet.xsd"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">  
</xsd:schema>  

合并 XML 中的数据Merging Data from XML

如果 DataSet 已经包含数据,则会向已存在于 DataSet 中的数据添加 XML 中的新数据。If the DataSet already contains data, the new data from the XML is added to the data already present in the DataSet. ReadXml 不会从 XML 合并到 DataSet 具有匹配主键的任何行信息。ReadXml does not merge from the XML into the DataSet any row information with matching primary keys. 若要使用 XML 中的新信息覆盖现有行信息,请使用 ReadXml 创建新的 DataSet ,然后将 Merge 新的 DataSet 引入现有 DataSetTo overwrite existing row information with new information from XML, use ReadXml to create a new DataSet, and then Merge the new DataSet into the existing DataSet. 请注意,使用ReadXMLdiffgramXmlReadMode加载 diffgram 将合并具有相同唯一标识符的行。Note that loading a DiffGram using ReadXML with an XmlReadMode of DiffGram will merge rows that have the same unique identifier.

请参阅See also