DataSet.ReadXml DataSet.ReadXml DataSet.ReadXml DataSet.ReadXml Method

定义

将 XML 架构和数据读入 DataSetReads XML schema and data into the DataSet.

重载

ReadXml(XmlReader, XmlReadMode) ReadXml(XmlReader, XmlReadMode) ReadXml(XmlReader, XmlReadMode) ReadXml(XmlReader, XmlReadMode)

使用指定的 DataSetXmlReader 将 XML 架构和数据读入 XmlReadModeReads XML schema and data into the DataSet using the specified XmlReader and XmlReadMode.

ReadXml(String, XmlReadMode) ReadXml(String, XmlReadMode) ReadXml(String, XmlReadMode) ReadXml(String, XmlReadMode)

使用指定的文件和 DataSet 将 XML 架构和数据读入 XmlReadModeReads XML schema and data into the DataSet using the specified file and XmlReadMode.

ReadXml(Stream, XmlReadMode) ReadXml(Stream, XmlReadMode) ReadXml(Stream, XmlReadMode)

使用指定的 DataSetStream 将 XML 架构和数据读入 XmlReadModeReads XML schema and data into the DataSet using the specified Stream and XmlReadMode.

ReadXml(TextReader, XmlReadMode) ReadXml(TextReader, XmlReadMode) ReadXml(TextReader, XmlReadMode) ReadXml(TextReader, XmlReadMode)

使用指定的 DataSetTextReader 将 XML 架构和数据读入 XmlReadModeReads XML schema and data into the DataSet using the specified TextReader and XmlReadMode.

ReadXml(String) ReadXml(String) ReadXml(String) ReadXml(String)

使用指定的文件将 XML 架构和数据读入 DataSetReads XML schema and data into the DataSet using the specified file.

ReadXml(TextReader) ReadXml(TextReader) ReadXml(TextReader) ReadXml(TextReader)

使用指定的 TextReader 将 XML 架构和数据读入 DataSetReads XML schema and data into the DataSet using the specified TextReader.

ReadXml(Stream) ReadXml(Stream) ReadXml(Stream)

使用指定的 Stream 将 XML 架构和数据读入 DataSetReads XML schema and data into the DataSet using the specified Stream.

ReadXml(XmlReader) ReadXml(XmlReader) ReadXml(XmlReader) ReadXml(XmlReader)

使用指定的 XmlReader 将 XML 架构和数据读入 DataSetReads XML schema and data into the DataSet using the specified XmlReader.

ReadXml(XmlReader, XmlReadMode) ReadXml(XmlReader, XmlReadMode) ReadXml(XmlReader, XmlReadMode) ReadXml(XmlReader, XmlReadMode)

使用指定的 DataSetXmlReader 将 XML 架构和数据读入 XmlReadModeReads XML schema and data into the DataSet using the specified XmlReader and XmlReadMode.

public:
 System::Data::XmlReadMode ReadXml(System::Xml::XmlReader ^ reader, System::Data::XmlReadMode mode);
public System.Data.XmlReadMode ReadXml (System.Xml.XmlReader reader, System.Data.XmlReadMode mode);
member this.ReadXml : System.Xml.XmlReader * System.Data.XmlReadMode -> System.Data.XmlReadMode
Public Function ReadXml (reader As XmlReader, mode As XmlReadMode) As XmlReadMode

参数

reader
XmlReader XmlReader XmlReader XmlReader

从中读取的 XmlReaderThe XmlReader from which to read.

返回

用于读取数据的 XmlReadModeThe XmlReadMode used to read the data.

注解

方法提供了一种方法,用于DataSetReadXmlSchema XML 文档只读取数据或将数据和架构读入到中,而方法只读取架构。 ReadXmlThe ReadXml method provides a way to read either data only, or both data and schema into a DataSet from an XML document, whereas the ReadXmlSchema method reads only the schema. 若要读取数据和架构,请使用ReadXML mode包含参数的重载之一,并将其值设置为ReadSchemaTo read both data and schema, use one of the ReadXML overloads that includes the mode parameter, and set its value to ReadSchema.

请注意,对于WriteXmlWriteXmlSchema方法也是如此。Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要从DataSet编写 XML 数据或同时编写架构和数据,请WriteXml使用方法。To write XML data, or both schema and data from the DataSet, use the WriteXml method. 若要只写入架构,请使用WriteXmlSchema方法。To write just the schema, use the WriteXmlSchema method.

备注

如果在中读取或写入的DataRow列类型实现IDynamicMetaObjectProvider且不实现IXmlSerializable,则将引发。InvalidOperationExceptionAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

如果指定了内嵌架构,则在加载数据之前,使用内联架构来扩展现有的关系结构。If an in-line schema is specified, the in-line schema is used to extend the existing relational structure prior to loading the data. 如果存在任何冲突(例如,使用不同的数据类型定义的同一表中的同一列),则会引发异常。If there are any conflicts (for example, the same column in the same table defined with different data types) an exception is raised.

如果未指定内联架构,根据 XML 文档的结构,将根据需要通过推理扩展关系结构。If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. 如果无法通过推理扩展架构以便公开所有数据,则会引发异常。If the schema cannot be extended through inference in order to expose all data, an exception is raised.

备注

DataSet序列化的 XML 中转义 xml 元素( DataColumn如(""))时,不会将该元素与相应的或DataTable关联。The DataSet does not associate an XML element with its corresponding DataColumn or DataTable when legal XML characters like ("") are escaped in the serialized XML. 本身DataSet只是转义 xml 元素名称中的非法 XML 字符,因此只能使用同一个。The DataSet itself only escapes illegal XML characters in XML element names and hence can only consume the same. 当转义 XML 元素名称中的法律字符时,将在处理时忽略元素。When legal characters in XML element name are escaped, the element is ignored while processing.

DataSet如果的 XML 架构targetNamespace包含,则数据可能不会被读取,并且在调用ReadXml以加载DataSet包含元素且无限定命名空间的 xml 时可能会遇到异常。If the XML Schema for a 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. 若要读取未限定的elementFormDefault元素,请在 XML 架构中设置等于 "合格的",如下面的示例所示。To read unqualified elements, set elementFormDefault equal to "qualified" in your XML Schema as the following example demonstrates.

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

备注

如果在同一命名空间DataSet中包含相同名称但不同类型的元素的架构,则当您尝试ReadXml通过指定XmlReadMode.ReadSchema将架构读取DataSet到时,将引发异常。If the schema for your DataSet contains elements of the same name, but different type, in the same namespace, an exception is thrown when you attempt to read the schema into the DataSet with ReadXml by specifying XmlReadMode.ReadSchema. 如果使用 .NET Framework 版本1.0,则不会发生此异常。This exception does not occur if you are using .NET Framework version 1.0.

另请参阅

ReadXml(String, XmlReadMode) ReadXml(String, XmlReadMode) ReadXml(String, XmlReadMode) ReadXml(String, XmlReadMode)

使用指定的文件和 DataSet 将 XML 架构和数据读入 XmlReadModeReads XML schema and data into the DataSet using the specified file and XmlReadMode.

public:
 System::Data::XmlReadMode ReadXml(System::String ^ fileName, System::Data::XmlReadMode mode);
public System.Data.XmlReadMode ReadXml (string fileName, System.Data.XmlReadMode mode);
member this.ReadXml : string * System.Data.XmlReadMode -> System.Data.XmlReadMode
Public Function ReadXml (fileName As String, mode As XmlReadMode) As XmlReadMode

参数

fileName
String String String String

作为读取源的文件名(包括路径)。The filename (including the path) from which to read.

返回

用于读取数据的 XmlReadModeThe XmlReadMode used to read the data.

异常

注解

方法提供了一种方法,用于DataSetReadXmlSchema XML 文档只读取数据或将数据和架构读入到中,而方法只读取架构。 ReadXmlThe ReadXml method provides a way to read either data only, or both data and schema into a DataSet from an XML document, whereas the ReadXmlSchema method reads only the schema. 若要读取数据和架构,请使用ReadXML mode包含参数的重载之一,并将其值设置为ReadSchemaTo read both data and schema, use one of the ReadXML overloads that includes the mode parameter, and set its value to ReadSchema.

请注意,对于WriteXmlWriteXmlSchema方法也是如此。Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要从DataSet编写 XML 数据或同时编写架构和数据,请WriteXml使用方法。To write XML data, or both schema and data from the DataSet, use the WriteXml method. 若要只写入架构,请使用WriteXmlSchema方法。To write just the schema, use the WriteXmlSchema method.

备注

如果在中读取或写入的DataRow列类型实现IDynamicMetaObjectProvider且不实现IXmlSerializable,则将引发。InvalidOperationExceptionAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

如果指定了内嵌架构,则在加载数据之前,使用内联架构来扩展现有的关系结构。If an in-line schema is specified, the in-line schema is used to extend the existing relational structure prior to loading the data. 如果存在任何冲突(例如,使用不同的数据类型定义的同一表中的同一列),则会引发异常。If there are any conflicts (for example, the same column in the same table defined with different data types) an exception is raised.

如果未指定内联架构,根据 XML 文档的结构,将根据需要通过推理扩展关系结构。If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. 如果无法通过推理扩展架构以便公开所有数据,则会引发异常。If the schema cannot be extended through inference in order to expose all data, an exception is raised.

备注

DataSet序列化的 XML 中转义 xml 元素( DataColumn如(""))时,不会将该元素与相应的或DataTable关联。The DataSet does not associate an XML element with its corresponding DataColumn or DataTable when legal XML characters like ("") are escaped in the serialized XML. 本身DataSet只是转义 xml 元素名称中的非法 XML 字符,因此只能使用同一个。The DataSet itself only escapes illegal XML characters in XML element names and hence can only consume the same. 当转义 XML 元素名称中的法律字符时,将在处理时忽略元素。When legal characters in XML element name are escaped, the element is ignored while processing.

DataSet如果的 XML 架构targetNamespace包含,则数据可能不会被读取,并且在调用ReadXml以加载DataSet包含元素且无限定命名空间的 xml 时可能会遇到异常。If the XML Schema for a 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. 若要读取未限定的elementFormDefault元素,请在 XML 架构中设置等于 "合格的",如下面的示例所示。To read unqualified elements, set elementFormDefault equal to "qualified" in your XML Schema as the following example demonstrates.

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

备注

如果在同一命名空间DataSet中包含相同名称但不同类型的元素的架构,则当您尝试ReadXml通过指定XmlReadMode.ReadSchema将架构读取DataSet到时,将引发异常。If the schema for your DataSet contains elements of the same name, but different type, in the same namespace, an exception is thrown when you attempt to read the schema into the DataSet with ReadXml by specifying XmlReadMode.ReadSchema. 如果使用 .NET Framework 版本1.0,则不会发生此异常。This exception does not occur if you are using .NET Framework version 1.0.

安全性

FileIOPermission
用于读取和写入文件。for reading and writing files. 关联的枚举:FileIOPermissionAccessAssociated enumeration: FileIOPermissionAccess

另请参阅

ReadXml(Stream, XmlReadMode) ReadXml(Stream, XmlReadMode) ReadXml(Stream, XmlReadMode)

使用指定的 DataSetStream 将 XML 架构和数据读入 XmlReadModeReads XML schema and data into the DataSet using the specified Stream and XmlReadMode.

public:
 System::Data::XmlReadMode ReadXml(System::IO::Stream ^ stream, System::Data::XmlReadMode mode);
public System.Data.XmlReadMode ReadXml (System.IO.Stream stream, System.Data.XmlReadMode mode);
member this.ReadXml : System.IO.Stream * System.Data.XmlReadMode -> System.Data.XmlReadMode

参数

stream
Stream Stream Stream Stream

从中读取的 StreamThe Stream from which to read.

返回

用于读取数据的 XmlReadModeThe XmlReadMode used to read the data.

注解

方法提供了一种方法,用于DataSetReadXmlSchema XML 文档只读取数据或将数据和架构读入到中,而方法只读取架构。 ReadXmlThe ReadXml method provides a way to read either data only, or both data and schema into a DataSet from an XML document, whereas the ReadXmlSchema method reads only the schema. 若要读取数据和架构,请使用ReadXML mode包含参数的重载之一,并将其值设置为ReadSchemaTo read both data and schema, use one of the ReadXML overloads that includes the mode parameter, and set its value to ReadSchema.

对于WriteXmlWriteXmlSchema方法也是如此。The same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要从DataSet编写 XML 数据或同时编写架构和数据,请WriteXml使用方法。To write XML data, or both schema and data from the DataSet, use the WriteXml method. 若要只写入架构,请使用WriteXmlSchema方法。To write just the schema, use the WriteXmlSchema method.

备注

ReadXml使用并且将设置Diffgram XmlReadMode为时,目标DataSet和原始DataSet内容的内容可能会因生成和处理 diffgram 的方式而有所不同。When you use ReadXml and you set XmlReadMode to Diffgram, the content of the target DataSet and the original DataSet may differ because of how the diffgram is generated and processed. 有关 diffgram 的详细信息,请参阅diffgramFor more information on diffgrams, see DiffGrams.

备注

如果在中读取或写入的DataRow列类型实现IDynamicMetaObjectProvider且不实现IXmlSerializable,则将引发。InvalidOperationExceptionAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

如果指定了内嵌架构,则在加载数据之前,使用内联架构来扩展现有的关系结构。If an in-line schema is specified, the in-line schema is used to extend the existing relational structure prior to loading the data. 如果存在任何冲突(例如,使用不同的数据类型定义的同一表中的同一列),则会引发异常。If there are any conflicts (for example, the same column in the same table defined with different data types) an exception is raised.

如果未指定内联架构,根据 XML 文档的结构,将根据需要通过推理扩展关系结构。If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. 如果无法通过推理扩展架构以便公开所有数据,则会引发异常。If the schema cannot be extended through inference in order to expose all data, an exception is raised.

备注

DataSet序列化的 XML 中转义 xml 元素( DataColumn如(""))时,不会将该元素与相应的或DataTable关联。The DataSet does not associate an XML element with its corresponding DataColumn or DataTable when legal XML characters like ("") are escaped in the serialized XML. 本身DataSet只是转义 xml 元素名称中的非法 XML 字符,因此只能使用同一个。The DataSet itself only escapes illegal XML characters in XML element names and hence can only consume the same. 当转义 XML 元素名称中的法律字符时,将在处理时忽略元素。When legal characters in XML element name are escaped, the element is ignored while processing.

如果DataSet包含targetNamespace的 XML 架构,则数据可能不会被读取,并且在调用ReadXml以加载DataSet包含元素且无限定命名空间的 xml 时可能会遇到异常。If the XML Schema for a DataSet includes 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. 若要读取未限定的elementFormDefault元素,请在 XML 架构中设置等于 "合格的",如下面的示例所示。To read unqualified elements, set elementFormDefault equal to "qualified" in your XML Schema as the following example demonstrates.

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

备注

如果在同一命名空间DataSet中包含相同名称但不同类型的元素的架构,则当您尝试ReadXml通过指定XmlReadMode.ReadSchema将架构读取DataSet到时,将引发异常。If the schema for your DataSet contains elements of the same name, but different type, in the same namespace, an exception is thrown when you attempt to read the schema into the DataSet with ReadXml by specifying XmlReadMode.ReadSchema. 如果使用 .NET Framework 版本1.0,则不会发生此异常。This exception does not occur if you are using .NET Framework version 1.0.

另请参阅

ReadXml(TextReader, XmlReadMode) ReadXml(TextReader, XmlReadMode) ReadXml(TextReader, XmlReadMode) ReadXml(TextReader, XmlReadMode)

使用指定的 DataSetTextReader 将 XML 架构和数据读入 XmlReadModeReads XML schema and data into the DataSet using the specified TextReader and XmlReadMode.

public:
 System::Data::XmlReadMode ReadXml(System::IO::TextReader ^ reader, System::Data::XmlReadMode mode);
public System.Data.XmlReadMode ReadXml (System.IO.TextReader reader, System.Data.XmlReadMode mode);
member this.ReadXml : System.IO.TextReader * System.Data.XmlReadMode -> System.Data.XmlReadMode
Public Function ReadXml (reader As TextReader, mode As XmlReadMode) As XmlReadMode

参数

reader
TextReader TextReader TextReader TextReader

从中读取的 TextReaderThe TextReader from which to read.

返回

用于读取数据的 XmlReadModeThe XmlReadMode used to read the data.

注解

方法提供了一种方法,用于DataSetReadXmlSchema XML 文档只读取数据或将数据和架构读入到中,而方法只读取架构。 ReadXmlThe ReadXml method provides a way to read either data only, or both data and schema into a DataSet from an XML document, whereas the ReadXmlSchema method reads only the schema. 若要读取数据和架构,请使用ReadXML mode包含参数的重载之一,并将其值设置为ReadSchemaTo read both data and schema, use one of the ReadXML overloads that includes the mode parameter, and set its value to ReadSchema.

请注意,对于WriteXmlWriteXmlSchema方法也是如此。Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要从DataSet编写 XML 数据或同时编写架构和数据,请WriteXml使用方法。To write XML data, or both schema and data from the DataSet, use the WriteXml method. 若要只写入架构,请使用WriteXmlSchema方法。To write just the schema, use the WriteXmlSchema method.

备注

如果在中读取或写入的DataRow列类型实现IDynamicMetaObjectProvider且不实现IXmlSerializable,则将引发。InvalidOperationExceptionAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

如果指定了内嵌架构,则在加载数据之前,使用内联架构来扩展现有的关系结构。If an in-line schema is specified, the in-line schema is used to extend the existing relational structure prior to loading the data. 如果存在任何冲突(例如,使用不同的数据类型定义的同一表中的同一列),则会引发异常。If there are any conflicts (for example, the same column in the same table defined with different data types) an exception is raised.

如果未指定内联架构,根据 XML 文档的结构,将根据需要通过推理扩展关系结构。If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. 如果无法通过推理扩展架构以便公开所有数据,则会引发异常。If the schema cannot be extended through inference in order to expose all data, an exception is raised.

备注

DataSet序列化的 XML 中转义 xml 元素( DataColumn如(""))时,不会将该元素与相应的或DataTable关联。The DataSet does not associate an XML element with its corresponding DataColumn or DataTable when legal XML characters like ("") are escaped in the serialized XML. 本身DataSet只是转义 xml 元素名称中的非法 XML 字符,因此只能使用同一个。The DataSet itself only escapes illegal XML characters in XML element names and hence can only consume the same. 当转义 XML 元素名称中的法律字符时,将在处理时忽略元素。When legal characters in XML element name are escaped, the element is ignored while processing.

如果DataSet包含targetNamespace的 XML 架构,则数据可能不会被读取,并且在调用ReadXml以加载DataSet包含元素且无限定命名空间的 xml 时可能会遇到异常。If the XML Schema for a DataSet includes 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. 若要读取未限定的elementFormDefault元素,请在 XML 架构中设置等于 "合格的",如下面的示例所示。To read unqualified elements, set elementFormDefault equal to "qualified" in your XML Schema as the following example demonstrates.

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

备注

如果在同一命名空间DataSet中包含相同名称但不同类型的元素的架构,则当您尝试ReadXml通过指定XmlReadMode.ReadSchema将架构读取DataSet到时,将引发异常。If the schema for your DataSet contains elements of the same name, but different type, in the same namespace, an exception is thrown when you attempt to read the schema into the DataSet with ReadXml by specifying XmlReadMode.ReadSchema. 如果使用 .NET Framework 版本1.0,则不会发生此异常。This exception does not occur if you are using .NET Framework version 1.0.

另请参阅

ReadXml(String) ReadXml(String) ReadXml(String) ReadXml(String)

使用指定的文件将 XML 架构和数据读入 DataSetReads XML schema and data into the DataSet using the specified file.

public:
 System::Data::XmlReadMode ReadXml(System::String ^ fileName);
public System.Data.XmlReadMode ReadXml (string fileName);
member this.ReadXml : string -> System.Data.XmlReadMode
Public Function ReadXml (fileName As String) As XmlReadMode

参数

fileName
String String String String

作为读取源的文件名(包括路径)。The filename (including the path) from which to read.

返回

用于读取数据的 XmlReadModeThe XmlReadMode used to read the data.

异常

示例

下面的示例首先创建一个简单DataSet的, DataTable其中包含一个、两个列和10个行。The following example first creates a simple DataSet with one DataTable, two columns, and ten rows. 通过调用方法将DataSet架构和数据写入磁盘。 WriteXmlThe DataSet schema and data are written to disk by invoking the WriteXml method. 将创建DataSet第二个ReadXml ,并使用方法来填充架构和数据。A second DataSet is created and the ReadXml method is used to fill it with schema and data.

   private void DemonstrateReadWriteXMLDocumentWithStreamReader()
   {
       // Create a DataSet with one table and two columns.
       DataSet OriginalDataSet = new DataSet("dataSet");
       OriginalDataSet.Namespace= "NetFrameWork";
       DataTable table = new DataTable("table");
       DataColumn idColumn = new DataColumn("id", 
           Type.GetType("System.Int32"));
       idColumn.AutoIncrement= true;

       DataColumn itemColumn = new DataColumn("item");
       table.Columns.Add(idColumn);
       table.Columns.Add(itemColumn);
       OriginalDataSet.Tables.Add(table);

       // Add ten rows.
       DataRow newRow;
       for(int i = 0; i < 10; i++)
       {
           newRow = table.NewRow();
           newRow["item"]= "item " + i;
           table.Rows.Add(newRow);
       }
       OriginalDataSet.AcceptChanges();

       // Print out values of each table in the DataSet 
       // using the function defined below.
       PrintValues(OriginalDataSet, "Original DataSet");

       // Write the schema and data to an XML file.
       string xmlFilename = "XmlDocument.xml";

       // Use WriteXml to write the document.
       OriginalDataSet.WriteXml(xmlFilename);

       // Dispose of the original DataSet.
       OriginalDataSet.Dispose();

       // Create a new DataSet.
       DataSet newDataSet = new DataSet("New DataSet");

       // Read the XML document into the DataSet.
       newDataSet.ReadXml(xmlFilename);

       // Print out values of each table in the DataSet 
       // using the function defined below.
       PrintValues(newDataSet,"New DataSet");
   }

   private void PrintValues(DataSet dataSet, string label)
   {
       Console.WriteLine("\n" + label);
       foreach(DataTable table in dataSet.Tables)
       {
           Console.WriteLine("TableName: " + table.TableName);
           foreach(DataRow row in table.Rows)
           {
               foreach(DataColumn column in table.Columns)
               {
                   Console.Write("\table " + row[column] );
               }
               Console.WriteLine();
           }
       }
   }
Private Sub DemonstrateReadWriteXMLDocumentWithStreamReader()
    ' Create a DataSet with one table and two columns.
    Dim OriginalDataSet As New DataSet("dataSet")
    OriginalDataSet.Namespace = "NetFrameWork"
    Dim table As New DataTable("table")
    Dim idColumn As New DataColumn("id", _
        Type.GetType("System.Int32"))
    idColumn.AutoIncrement = True

    Dim itemColumn As New DataColumn("item")
    table.Columns.Add(idColumn)
    table.Columns.Add(itemColumn)
    OriginalDataSet.Tables.Add(table)

    ' Add ten rows.
    Dim newRow As DataRow
    Dim i As Integer
    For i = 0 To 9
        newRow = table.NewRow()
        newRow("item") = "item " & i.ToString()
        table.Rows.Add(newRow)
    Next i
    OriginalDataSet.AcceptChanges()

    ' Print out values of each table in the DataSet 
    ' using the function defined below.
    PrintValues(OriginalDataSet, "Original DataSet")

    ' Write the schema and data to an XML file.
    Dim xmlFilename As String = "XmlDocument.xml"

    ' Use WriteXml to write the document.
    OriginalDataSet.WriteXml(xmlFilename)

    ' Dispose of the original DataSet.
    OriginalDataSet.Dispose()

    ' Create a new DataSet.
    Dim newDataSet As New DataSet("New DataSet")

    ' Read the XML document into the DataSet.
    newDataSet.ReadXml(xmlFilename)

    ' Print out values of each table in the DataSet 
    ' using the function defined below.
    PrintValues(newDataSet, "New DataSet")
End Sub
    
Private Sub PrintValues(dataSet As DataSet, label As String)
    Console.WriteLine(ControlChars.Cr & label)
    Dim table As DataTable
    For Each table In  dataSet.Tables
        Console.WriteLine("TableName: " & table.TableName)
        Dim row As DataRow
        For Each row In  table.Rows
            Dim column As DataColumn
            For Each column In  table.Columns
                Console.Write(ControlChars.Tab & " " & _
                    row(column).ToString())
            Next column
            Console.WriteLine()
        Next row
    Next table
End Sub

注解

方法提供了一种方法,用于DataSetReadXmlSchema XML 文档只读取数据或将数据和架构读入到中,而方法只读取架构。 ReadXmlThe ReadXml method provides a way to read either data only, or both data and schema into a DataSet from an XML document, whereas the ReadXmlSchema method reads only the schema. 若要读取数据和架构,请使用ReadXML mode包含参数的重载之一,并将其值设置为ReadSchemaTo read both data and schema, use one of the ReadXML overloads that includes the mode parameter, and set its value to ReadSchema.

请注意,对于WriteXmlWriteXmlSchema方法也是如此。Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要从DataSet编写 XML 数据或同时编写架构和数据,请WriteXml使用方法。To write XML data, or both schema and data from the DataSet, use the WriteXml method. 若要只写入架构,请使用WriteXmlSchema方法。To write just the schema, use the WriteXmlSchema method.

备注

如果在中读取或写入的DataRow列类型实现IDynamicMetaObjectProvider且不实现IXmlSerializable,则将引发。InvalidOperationExceptionAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

如果指定了内嵌架构,则在加载数据之前,使用内联架构来扩展现有的关系结构。If an in-line schema is specified, the in-line schema is used to extend the existing relational structure prior to loading the data. 如果存在任何冲突(例如,使用不同的数据类型定义的同一表中的同一列),则会引发异常。If there are any conflicts (for example, the same column in the same table defined with different data types) an exception is raised.

如果未指定内联架构,根据 XML 文档的结构,将根据需要通过推理扩展关系结构。If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. 如果无法通过推理扩展架构以便公开所有数据,则会引发异常。If the schema cannot be extended through inference in order to expose all data, an exception is raised.

备注

DataSet序列化的 XML 中转义 xml 元素( DataColumn如(""))时,不会将该元素与相应的或DataTable关联。The DataSet does not associate an XML element with its corresponding DataColumn or DataTable when legal XML characters like ("") are escaped in the serialized XML. 本身DataSet只是转义 xml 元素名称中的非法 XML 字符,因此只能使用同一个。The DataSet itself only escapes illegal XML characters in XML element names and hence can only consume the same. 当转义 XML 元素名称中的法律字符时,将在处理时忽略元素。When legal characters in XML element name are escaped, the element is ignored while processing.

DataSet如果的 XML 架构targetNamespace包含,则数据可能不会被读取,并且在调用ReadXml以加载DataSet包含元素且无限定命名空间的 xml 时可能会遇到异常。If the XML Schema for a 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. 若要读取未限定的elementFormDefault元素,请在 XML 架构中设置等于 "合格的",如下面的示例所示。To read unqualified elements, set elementFormDefault equal to "qualified" in your XML Schema as the following example demonstrates.

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

备注

如果在同一命名空间DataSet中包含相同名称但不同类型的元素的架构,则当您尝试ReadXml通过指定XmlReadMode.ReadSchema将架构读取DataSet到时,将引发异常。If the schema for your DataSet contains elements of the same name, but different type, in the same namespace, an exception is thrown when you attempt to read the schema into the DataSet with ReadXml by specifying XmlReadMode.ReadSchema. 如果使用 .NET Framework 版本1.0,则不会发生此异常。This exception does not occur if you are using .NET Framework version 1.0.

安全性

FileIOPermission
用于读取和写入文件。for reading and writing files. 关联的枚举:FileIOPermissionAccessAssociated enumeration: FileIOPermissionAccess

另请参阅

ReadXml(TextReader) ReadXml(TextReader) ReadXml(TextReader) ReadXml(TextReader)

使用指定的 TextReader 将 XML 架构和数据读入 DataSetReads XML schema and data into the DataSet using the specified TextReader.

public:
 System::Data::XmlReadMode ReadXml(System::IO::TextReader ^ reader);
public System.Data.XmlReadMode ReadXml (System.IO.TextReader reader);
member this.ReadXml : System.IO.TextReader -> System.Data.XmlReadMode
Public Function ReadXml (reader As TextReader) As XmlReadMode

参数

reader
TextReader TextReader TextReader TextReader

从中读取架构和数据的 TextReaderThe TextReader from which to read the schema and data.

返回

用于读取数据的 XmlReadModeThe XmlReadMode used to read the data.

示例

下面的示例首先创建一个简单DataSet的, DataTable其中包含一个、两个列和10个行。The following example first creates a simple DataSet with one DataTable, two columns, and ten rows. 通过调用方法将DataSet架构和数据写入磁盘。 WriteXmlThe DataSet schema and data are written to disk by invoking the WriteXml method. 将创建DataSet第二个ReadXml ,并使用方法来填充架构和数据。A second DataSet is created and the ReadXml method is used to fill it with schema and data.

   private void DemonstrateReadWriteXMLDocumentWithFileStream()
   {
       // Create a DataSet with one table and two columns.
       DataSet originalDataSet = new DataSet("dataSet");
       DataTable table = new DataTable("table");
       DataColumn idColumn = new DataColumn("id", 
           Type.GetType("System.Int32"));
       idColumn.AutoIncrement= true;

       DataColumn itemColumn = new DataColumn("item");
       table.Columns.Add(idColumn);
       table.Columns.Add(itemColumn);
       originalDataSet.Tables.Add(table);
       // Add ten rows.

       DataRow newRow;
       for(int i = 0; i < 10; i++)
       {
           newRow = table.NewRow();
           newRow["item"]= "item " + i;
           table.Rows.Add(newRow);
       }
       originalDataSet.AcceptChanges();

       // Print out values of each table in the DataSet  
       // using the function defined below.
       PrintValues(originalDataSet, "Original DataSet");

       // Write the schema and data to XML file with FileStream.
       string xmlFilename = "XmlDocument.xml";
       System.IO.FileStream streamWrite = new System.IO.FileStream
           (xmlFilename, System.IO.FileMode.Create);

       // Use WriteXml to write the XML document.
       originalDataSet.WriteXml(streamWrite);

       // Close the FileStream.
       streamWrite.Close();
      
       // Dispose of the original DataSet.
       originalDataSet.Dispose();
       // Create a new DataSet.
       DataSet newDataSet = new DataSet("New DataSet");
      
       // Read the XML document back in. 
       // Create new FileStream to read schema with.
       System.IO.FileStream streamRead = new System.IO.FileStream
           (xmlFilename,System.IO.FileMode.Open);
       newDataSet.ReadXml(streamRead);

       // Print out values of each table in the DataSet 
       // using the function defined below.
       PrintValues(newDataSet,"New DataSet");
   }

   private void PrintValues(DataSet dataSet, string label)
   {
       Console.WriteLine("\n" + label);
       foreach(DataTable table in dataSet.Tables)
       {
           Console.WriteLine("TableName: " + table.TableName);
           foreach(DataRow row in table.Rows)
           {
               foreach(DataColumn column in table.Columns)
               {
                   Console.Write("\table " + row[column] );
               }
               Console.WriteLine();
           }
       }
   }
Private Sub DemonstrateReadWriteXMLDocumentWithFileStream()
    ' Create a DataSet with one table and two columns.
    Dim originalDataSet As New DataSet("dataSet")
    Dim table As New DataTable("table")
    Dim idColumn As New DataColumn("id", _
       Type.GetType("System.Int32"))
    idColumn.AutoIncrement = True

    Dim itemColumn As New DataColumn("item")
    table.Columns.Add(idColumn)
    table.Columns.Add(itemColumn)
    originalDataSet.Tables.Add(table)

    ' Add ten rows.
    Dim newRow As DataRow
    Dim i As Integer
    For i = 0 To 9
        newRow = table.NewRow()
        newRow("item") = "item " & i.ToString()
        table.Rows.Add(newRow)
    Next i
    originalDataSet.AcceptChanges()

    ' Print out values of each table in the DataSet 
    ' using the function defined below.
    PrintValues(originalDataSet, "Original DataSet")

    ' Write the schema and data to XML file with FileStream.
    Dim xmlFilename As String = "XmlDocument.xml"
    Dim streamWrite As New System.IO.FileStream _
       (xmlFilename, System.IO.FileMode.Create)

    ' Use WriteXml to write the XML document.
    originalDataSet.WriteXml(streamWrite)

    ' Close the FileStream.
    streamWrite.Close()
     
    ' Dispose of the original DataSet.
    originalDataSet.Dispose()
    ' Create a new DataSet.
    Dim newDataSet As New DataSet("New DataSet")
       
    ' Read the XML document back in. 
    ' Create new FileStream to read schema with.
    Dim streamRead As New System.IO.FileStream _
       (xmlFilename, System.IO.FileMode.Open)
     
    newDataSet.ReadXml(streamRead)
    ' Print out values of each table in the DataSet  
    ' using the function defined below.
    PrintValues(newDataSet, "New DataSet")
End Sub
   
Private Sub PrintValues(dataSet As DataSet, label As String)
    Console.WriteLine(ControlChars.Cr & label)
    Dim table As DataTable
    Dim row As DataRow
    Dim column As DataColumn
    For Each table In  dataSet.Tables
        Console.WriteLine("TableName: " & table.TableName)         
        For Each row In  table.Rows             
            For Each column In  table.Columns
                Console.Write(ControlChars.Tab & " " & _
                   row(column).ToString())
            Next column
            Console.WriteLine()
        Next row
    Next table
End Sub

注解

方法提供了一种方法,用于DataSetReadXmlSchema XML 文档只读取数据或将数据和架构读入到中,而方法只读取架构。 ReadXmlThe ReadXml method provides a way to read either data only, or both data and schema into a DataSet from an XML document, whereas the ReadXmlSchema method reads only the schema. 若要读取数据和架构,请使用ReadXML mode包含参数的重载之一,并将其值设置为ReadSchemaTo read both data and schema, use one of the ReadXML overloads that includes the mode parameter, and set its value to ReadSchema.

请注意,对于WriteXmlWriteXmlSchema方法也是如此。Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要从DataSet编写 XML 数据或同时编写架构和数据,请WriteXml使用方法。To write XML data, or both schema and data from the DataSet, use the WriteXml method. 若要只写入架构,请使用WriteXmlSchema方法。To write just the schema, use the WriteXmlSchema method.

备注

如果在中读取或写入的DataRow列类型实现IDynamicMetaObjectProvider且不实现IXmlSerializable,则将引发。InvalidOperationExceptionAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

如果指定了内嵌架构,则在加载数据之前,使用内联架构来扩展现有的关系结构。If an in-line schema is specified, the in-line schema is used to extend the existing relational structure prior to loading the data. 如果存在任何冲突(例如,使用不同的数据类型定义的同一表中的同一列),则会引发异常。If there are any conflicts (for example, the same column in the same table defined with different data types) an exception is raised.

如果未指定内联架构,根据 XML 文档的结构,将根据需要通过推理扩展关系结构。If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. 如果无法通过推理扩展架构以便公开所有数据,则会引发异常。If the schema cannot be extended through inference in order to expose all data, an exception is raised.

备注

DataSet序列化的 XML 中转义 xml 元素( DataColumn如(""))时,不会将该元素与相应的或DataTable关联。The DataSet does not associate an XML element with its corresponding DataColumn or DataTable when legal XML characters like ("") are escaped in the serialized XML. 本身DataSet只是转义 xml 元素名称中的非法 XML 字符,因此只能使用同一个。The DataSet itself only escapes illegal XML characters in XML element names and hence can only consume the same. 当转义 XML 元素名称中的法律字符时,将在处理时忽略元素。When legal characters in XML element name are escaped, the element is ignored while processing.

如果DataSet包含targetNamespace的 XML 架构,则数据可能不会被读取,并且在调用ReadXml以加载DataSet包含元素且无限定命名空间的 xml 时可能会遇到异常。If the XML Schema for a DataSet includes 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. 若要读取未限定的elementFormDefault元素,请在 XML 架构中设置等于 "合格的",如下面的示例所示。To read unqualified elements, set elementFormDefault equal to "qualified" in your XML Schema as the following example demonstrates.

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

继承自TextReader类的类StreamReader包括和StringReader类。Classes that inherit from the TextReader class include the StreamReader and StringReader classes.

备注

如果在同一命名空间DataSet中包含相同名称但不同类型的元素的架构,则当您尝试ReadXml通过指定XmlReadMode.ReadSchema将架构读取DataSet到时,将引发异常。If the schema for your DataSet contains elements of the same name, but different type, in the same namespace, an exception is thrown when you attempt to read the schema into the DataSet with ReadXml by specifying XmlReadMode.ReadSchema. 如果使用 .NET Framework 版本1.0,则不会发生此异常。This exception does not occur if you are using .NET Framework version 1.0.

另请参阅

ReadXml(Stream) ReadXml(Stream) ReadXml(Stream)

使用指定的 Stream 将 XML 架构和数据读入 DataSetReads XML schema and data into the DataSet using the specified Stream.

public:
 System::Data::XmlReadMode ReadXml(System::IO::Stream ^ stream);
public System.Data.XmlReadMode ReadXml (System.IO.Stream stream);
member this.ReadXml : System.IO.Stream -> System.Data.XmlReadMode

参数

stream
Stream Stream Stream Stream

Stream 派生的对象。An object that derives from Stream.

返回

用于读取数据的 XmlReadModeThe XmlReadMode used to read the data.

示例

下面的示例首先创建一个简单DataSet的, DataTable其中包含一个、两个列和10个行。The following example first creates a simple DataSet with one DataTable, two columns, and ten rows. 通过调用方法将DataSet架构和数据写入磁盘。 WriteXmlThe DataSet schema and data are written to disk by invoking the WriteXml method. 将创建DataSet第二个ReadXml ,并使用方法来填充架构和数据。A second DataSet is created and the ReadXml method is used to fill it with schema and data.

   private void DemonstrateReadWriteXMLDocumentWithFileStream()
   {
       // Create a DataSet with one table and two columns.
       DataSet originalDataSet = new DataSet("dataSet");
       DataTable table = new DataTable("table");
       DataColumn idColumn = new DataColumn("id", 
           Type.GetType("System.Int32"));
       idColumn.AutoIncrement= true;

       DataColumn itemColumn = new DataColumn("item");
       table.Columns.Add(idColumn);
       table.Columns.Add(itemColumn);
       originalDataSet.Tables.Add(table);
       // Add ten rows.

       DataRow newRow;
       for(int i = 0; i < 10; i++)
       {
           newRow = table.NewRow();
           newRow["item"]= "item " + i;
           table.Rows.Add(newRow);
       }
       originalDataSet.AcceptChanges();

       // Print out values of each table in the DataSet  
       // using the function defined below.
       PrintValues(originalDataSet, "Original DataSet");

       // Write the schema and data to XML file with FileStream.
       string xmlFilename = "XmlDocument.xml";
       System.IO.FileStream streamWrite = new System.IO.FileStream
           (xmlFilename, System.IO.FileMode.Create);

       // Use WriteXml to write the XML document.
       originalDataSet.WriteXml(streamWrite);

       // Close the FileStream.
       streamWrite.Close();
      
       // Dispose of the original DataSet.
       originalDataSet.Dispose();
       // Create a new DataSet.
       DataSet newDataSet = new DataSet("New DataSet");
      
       // Read the XML document back in. 
       // Create new FileStream to read schema with.
       System.IO.FileStream streamRead = new System.IO.FileStream
           (xmlFilename,System.IO.FileMode.Open);
       newDataSet.ReadXml(streamRead);

       // Print out values of each table in the DataSet 
       // using the function defined below.
       PrintValues(newDataSet,"New DataSet");
   }

   private void PrintValues(DataSet dataSet, string label)
   {
       Console.WriteLine("\n" + label);
       foreach(DataTable table in dataSet.Tables)
       {
           Console.WriteLine("TableName: " + table.TableName);
           foreach(DataRow row in table.Rows)
           {
               foreach(DataColumn column in table.Columns)
               {
                   Console.Write("\table " + row[column] );
               }
               Console.WriteLine();
           }
       }
   }
Private Sub DemonstrateReadWriteXMLDocumentWithFileStream()
    ' Create a DataSet with one table and two columns.
    Dim originalDataSet As New DataSet("dataSet")
    Dim table As New DataTable("table")
    Dim idColumn As New DataColumn("id", _
       Type.GetType("System.Int32"))
    idColumn.AutoIncrement = True

    Dim itemColumn As New DataColumn("item")
    table.Columns.Add(idColumn)
    table.Columns.Add(itemColumn)
    originalDataSet.Tables.Add(table)

    ' Add ten rows.
    Dim newRow As DataRow
    Dim i As Integer
    For i = 0 To 9
        newRow = table.NewRow()
        newRow("item") = "item " & i.ToString()
        table.Rows.Add(newRow)
    Next i
    originalDataSet.AcceptChanges()

    ' Print out values of each table in the DataSet 
    ' using the function defined below.
    PrintValues(originalDataSet, "Original DataSet")

    ' Write the schema and data to XML file with FileStream.
    Dim xmlFilename As String = "XmlDocument.xml"
    Dim streamWrite As New System.IO.FileStream _
       (xmlFilename, System.IO.FileMode.Create)

    ' Use WriteXml to write the XML document.
    originalDataSet.WriteXml(streamWrite)

    ' Close the FileStream.
    streamWrite.Close()
     
    ' Dispose of the original DataSet.
    originalDataSet.Dispose()
    ' Create a new DataSet.
    Dim newDataSet As New DataSet("New DataSet")
       
    ' Read the XML document back in. 
    ' Create new FileStream to read schema with.
    Dim streamRead As New System.IO.FileStream _
       (xmlFilename, System.IO.FileMode.Open)
     
    newDataSet.ReadXml(streamRead)
    ' Print out values of each table in the DataSet  
    ' using the function defined below.
    PrintValues(newDataSet, "New DataSet")
End Sub
   
Private Sub PrintValues(dataSet As DataSet, label As String)
    Console.WriteLine(ControlChars.Cr & label)
    Dim table As DataTable
    Dim row As DataRow
    Dim column As DataColumn
    For Each table In  dataSet.Tables
        Console.WriteLine("TableName: " & table.TableName)         
        For Each row In  table.Rows             
            For Each column In  table.Columns
                Console.Write(ControlChars.Tab & " " & _
                   row(column).ToString())
            Next column
            Console.WriteLine()
        Next row
    Next table
End Sub

注解

方法提供了一种方法,用于DataSetReadXmlSchema XML 文档只读取数据或将数据和架构读入到中,而方法只读取架构。 ReadXmlThe ReadXml method provides a way to read either data only, or both data and schema into a DataSet from an XML document, whereas the ReadXmlSchema method reads only the schema. 若要读取数据和架构,请使用ReadXML mode包含参数的重载之一,并将其值设置为ReadSchemaTo read both data and schema, use one of the ReadXML overloads that includes the mode parameter, and set its value to ReadSchema.

请注意,对于WriteXmlWriteXmlSchema方法也是如此。Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要从DataSet编写 XML 数据或同时编写架构和数据,请WriteXml使用方法。To write XML data, or both schema and data from the DataSet, use the WriteXml method. 若要只写入架构,请使用WriteXmlSchema方法。To write just the schema, use the WriteXmlSchema method.

备注

如果在中读取或写入的DataRow列类型实现IDynamicMetaObjectProvider且不实现IXmlSerializable,则将引发。InvalidOperationExceptionAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

如果指定了内嵌架构,则在加载数据之前,使用内联架构来扩展现有的关系结构。If an in-line schema is specified, the in-line schema is used to extend the existing relational structure prior to loading the data. 如果存在任何冲突(例如,使用不同的数据类型定义的同一表中的同一列),则会引发异常。If there are any conflicts (for example, the same column in the same table defined with different data types) an exception is raised.

如果未指定内联架构,根据 XML 文档的结构,将根据需要通过推理扩展关系结构。If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. 如果无法通过推理扩展架构以便公开所有数据,则会引发异常。If the schema cannot be extended through inference in order to expose all data, an exception is raised.

备注

DataSet序列化的 XML 中转义 xml 元素( DataColumn如(""))时,不会将该元素与相应的或DataTable关联。The DataSet does not associate an XML element with its corresponding DataColumn or DataTable when legal XML characters like ("") are escaped in the serialized XML. 本身DataSet只是转义 xml 元素名称中的非法 XML 字符,因此只能使用同一个。The DataSet itself only escapes illegal XML characters in XML element names and hence can only consume the same. 当转义 XML 元素名称中的法律字符时,将在处理时忽略元素。When legal characters in XML element name are escaped, the element is ignored while processing.

如果DataSet包含targetNamespace的 XML 架构,则数据可能不会被读取,并且在调用ReadXml以加载DataSet包含元素且无限定命名空间的 xml 时可能会遇到异常。If the XML Schema for a DataSet includes 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. 若要读取未限定的elementFormDefault元素,请在 XML 架构中设置等于 "合格的",如下面的示例所示。To read unqualified elements, set elementFormDefault equal to "qualified" in your XML Schema, as the following example demonstrates.

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

备注

如果在同一命名空间DataSet中包含相同名称但不同类型的元素的架构,则当您尝试ReadXml通过指定XmlReadMode.ReadSchema将架构读取DataSet到时,将引发异常。If the schema for your DataSet contains elements of the same name, but different type, in the same namespace, an exception is thrown when you attempt to read the schema into the DataSet with ReadXml by specifying XmlReadMode.ReadSchema. 如果使用 .NET Framework 版本1.0,则不会发生此异常。This exception does not occur if you are using .NET Framework version 1.0.

另请参阅

ReadXml(XmlReader) ReadXml(XmlReader) ReadXml(XmlReader) ReadXml(XmlReader)

使用指定的 XmlReader 将 XML 架构和数据读入 DataSetReads XML schema and data into the DataSet using the specified XmlReader.

public:
 System::Data::XmlReadMode ReadXml(System::Xml::XmlReader ^ reader);
public System.Data.XmlReadMode ReadXml (System.Xml.XmlReader reader);
member this.ReadXml : System.Xml.XmlReader -> System.Data.XmlReadMode
Public Function ReadXml (reader As XmlReader) As XmlReadMode

参数

reader
XmlReader XmlReader XmlReader XmlReader

从中读取的 XmlReaderThe XmlReader from which to read.

返回

用于读取数据的 XmlReadModeThe XmlReadMode used to read the data.

示例

下面的示例首先创建一个简单DataSet的, DataTable其中包含一个、两个列和10个行。The following example first creates a simple DataSet with one DataTable, two columns, and ten rows. 通过调用方法将DataSet架构和数据写入磁盘。 WriteXmlThe DataSet schema and data are written to disk by invoking the WriteXml method. 将创建DataSet第二个ReadXml ,并使用方法来填充架构和数据。A second DataSet is created and the ReadXml method is used to fill it with schema and data.

   private void DemonstrateReadWriteXMLDocumentWithXMLReader()
   {
       // Create a DataSet with one table and two columns.
       DataSet OriginalDataSet = new DataSet("dataSet");
       OriginalDataSet.Namespace= "NetFrameWork";
       DataTable table = new DataTable("table");
       DataColumn idColumn = new DataColumn("id", 
           Type.GetType("System.Int32"));
       idColumn.AutoIncrement= true;

       DataColumn itemColumn = new DataColumn("item");
       table.Columns.Add(idColumn);
       table.Columns.Add(itemColumn);
       OriginalDataSet.Tables.Add(table);

       // Add ten rows.
       DataRow newRow;
       for(int i = 0; i < 10; i++)
       {
           newRow = table.NewRow();
           newRow["item"]= "item " + i;
           table.Rows.Add(newRow);
       }
       OriginalDataSet.AcceptChanges();

       // Print out values of each table in the DataSet 
       // using the function defined below.
       PrintValues(OriginalDataSet, "Original DataSet");

       // Write the XML schema and data to file with FileStream.
       string xmlFilename = "XmlDocument.xml";

       // Create FileStream    
       System.IO.FileStream fsWriteXml = 
           new System.IO.FileStream
           (xmlFilename, System.IO.FileMode.Create);

       // Create an XmlTextWriter to write the file.
       System.Xml.XmlTextWriter xmlWriter = 
           new System.Xml.XmlTextWriter
           (fsWriteXml, System.Text.Encoding.Unicode);

       // Use WriteXml to write the document.
       OriginalDataSet.WriteXml(xmlWriter, XmlWriteMode.WriteSchema); 

       // Close the FileStream.
       fsWriteXml.Close();
      
       // Dispose of the original DataSet.
       OriginalDataSet.Dispose();

       // Create a new DataSet.
       DataSet newDataSet = new DataSet("New DataSet");
      
       // Read the XML document back in. 
       // Create new FileStream to read schema with.
       System.IO.FileStream fsReadXml = 
           new System.IO.FileStream
           (xmlFilename, System.IO.FileMode.Open);

       // Create an XmlTextReader to read the file.
       System.Xml.XmlTextReader xmlReader = 
           new System.Xml.XmlTextReader(fsReadXml);

       // Read the XML document into the DataSet.
       newDataSet.ReadXml(xmlReader, XmlReadMode.ReadSchema);

       // Close the XmlTextReader
       xmlReader.Close();

       // Print out values of each table in the DataSet 
       // fusing the unction defined below.
       PrintValues(newDataSet,"New DataSet");
   }

   private void PrintValues(DataSet dataSet, string label)
   {
       Console.WriteLine("\n" + label);
       foreach(DataTable table in dataSet.Tables)
       {
           Console.WriteLine("TableName: " + table.TableName);
           foreach(DataRow row in table.Rows)
           {
               foreach(DataColumn column in table.Columns)
               {
                   Console.Write("\t{0}({1})='{2}'", column.ColumnName, column.DataType.Name, row[column]);
               }
               Console.WriteLine();
           }
       }
   }
Private Sub DemonstrateReadWriteXMLDocumentWithXMLReader()
    ' Create a DataSet with one table and two columns.
    Dim OriginalDataSet As New DataSet("dataSet")
    OriginalDataSet.Namespace = "NetFrameWork"
    Dim table As New DataTable("table")
    Dim idColumn As New DataColumn("id", _
        Type.GetType("System.Int32"))
    idColumn.AutoIncrement = True

    Dim itemColumn As New DataColumn("item")
    table.Columns.Add(idColumn)
    table.Columns.Add(itemColumn)
    OriginalDataSet.Tables.Add(table)

    ' Add ten rows.
    Dim newRow As DataRow
    Dim i As Integer
    For i = 0 To 9
        newRow = table.NewRow()
        newRow("item") = "item " & i.ToString()
        table.Rows.Add(newRow)
    Next i
    OriginalDataSet.AcceptChanges()

    ' Print out values of each table in the DataSet 
    ' using the function defined below.
    PrintValues(OriginalDataSet, "Original DataSet")

    ' Write the XML schema and data to file with FileStream.
    Dim xmlFilename As String = "XmlDocument.xml"

    ' Create FileStream    
    Dim fsWriteXml As New System.IO.FileStream _
        (xmlFilename, System.IO.FileMode.Create)

    ' Create an XmlTextWriter to write the file.
    Dim xmlWriter As New System.Xml.XmlTextWriter _
        (fsWriteXml, System.Text.Encoding.Unicode)

    ' Use WriteXml to write the document.
    OriginalDataSet.WriteXml(xmlWriter, XmlWriteMode.WriteSchema)

    ' Close the FileStream.
    fsWriteXml.Close()
        
    ' Dispose of the original DataSet.
    OriginalDataSet.Dispose()

    ' Create a new DataSet.
    Dim newDataSet As New DataSet("New DataSet")
        
    ' Read the XML document back in. 
    ' Create new FileStream to read schema with.
    Dim fsReadXml As New System.IO.FileStream _
        (xmlFilename, System.IO.FileMode.Open)

    ' Create an XmlTextReader to read the file.
    Dim xmlReader As New System.Xml.XmlTextReader(fsReadXml)

    ' Read the XML document into the DataSet.
    newDataSet.ReadXml(xmlReader, XmlReadMode.ReadSchema)

    ' Close the XmlTextReader
    xmlReader.Close()
        
    ' Print out values of each table in the DataSet using the 
    ' function defined below.
    PrintValues(newDataSet, "New DataSet")
End Sub
    
    
 Private Sub PrintValues(dataSet As DataSet, label As String)
     Console.WriteLine(ControlChars.Cr & label)
     Dim table As DataTable
     Dim row As DataRow
     Dim column As DataColumn
     For Each table In  dataSet.Tables
         Console.WriteLine("TableName: " & table.TableName)
         For Each row In  table.Rows
             For Each column In  table.Columns
                 Console.Write(ControlChars.Tab & "{0}({1})='{2}'", column.ColumnName, column.DataType.Name, row(column).ToString())

             Next column
             Console.WriteLine()
         Next row
     Next table
  End Sub

注解

方法提供了一种方法,用于DataSetReadXmlSchema XML 文档只读取数据或将数据和架构读入到中,而方法只读取架构。 ReadXmlThe ReadXml method provides a way to read either data only, or both data and schema into a DataSet from an XML document, whereas the ReadXmlSchema method reads only the schema. 若要读取数据和架构,请使用ReadXML mode包含参数的重载之一,并将其值设置为ReadSchemaTo read both data and schema, use one of the ReadXML overloads that includes the mode parameter, and set its value to ReadSchema.

请注意,对于WriteXmlWriteXmlSchema方法也是如此。Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要从DataSet编写 XML 数据或同时编写架构和数据,请WriteXml使用方法。To write XML data, or both schema and data from the DataSet, use the WriteXml method. 若要只写入架构,请使用WriteXmlSchema方法。To write just the schema, use the WriteXmlSchema method.

备注

如果在中读取或写入的DataRow列类型实现IDynamicMetaObjectProvider且不实现IXmlSerializable,则将引发。InvalidOperationExceptionAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

如果指定了内嵌架构,则在加载数据之前,使用内联架构来扩展现有的关系结构。If an in-line schema is specified, the in-line schema is used to extend the existing relational structure prior to loading the data. 如果存在任何冲突(例如,使用不同的数据类型定义的同一表中的同一列),则会引发异常。If there are any conflicts (for example, the same column in the same table defined with different data types) an exception is raised.

如果未指定内联架构,根据 XML 文档的结构,将根据需要通过推理扩展关系结构。If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. 如果无法通过推理扩展架构以便公开所有数据,则会引发异常。If the schema cannot be extended through inference in order to expose all data, an exception is raised.

备注

DataSet序列化的 XML 中转义 xml 元素( DataColumn如(""))时,不会将该元素与相应的或DataTable关联。The DataSet does not associate an XML element with its corresponding DataColumn or DataTable when legal XML characters like ("") are escaped in the serialized XML. 本身DataSet只是转义 xml 元素名称中的非法 XML 字符,因此只能使用同一个。The DataSet itself only escapes illegal XML characters in XML element names and hence can only consume the same. 当转义 XML 元素名称中的法律字符时,将在处理时忽略元素。When legal characters in XML element name are escaped, the element is ignored while processing.

DataSet如果的 XML 架构targetNamespace包含,则数据可能不会被读取,并且在调用ReadXml以加载DataSet包含元素且无限定命名空间的 xml 时可能会遇到异常。If the XML Schema for a 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. 若要读取未限定的elementFormDefault元素,请在 XML 架构中设置等于 "合格的",如下面的示例所示。To read unqualified elements, set elementFormDefault equal to "qualified" in your XML Schema as the following example demonstrates.

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

System.Xml.XmlTextReader 继承自 XmlReaderSystem.Xml.XmlTextReader inherits from XmlReader.

备注

如果在同一命名空间DataSet中包含相同名称但不同类型的元素的架构,则当您尝试ReadXml通过指定XmlReadMode.ReadSchema将架构读取DataSet到时,将引发异常。If the schema for your DataSet contains elements of the same name, but different type, in the same namespace, an exception is thrown when you attempt to read the schema into the DataSet with ReadXml by specifying XmlReadMode.ReadSchema. 如果使用 .NET Framework 版本1.0,则不会发生此异常。This exception does not occur if you are using .NET Framework version 1.0.

另请参阅

适用于