DataSet.ReadXml 方法

定义

将 XML 架构和数据读入 DataSet

重载

ReadXml(XmlReader, XmlReadMode)

使用指定的 DataSetXmlReader 将 XML 架构和数据读入 XmlReadMode

ReadXml(String, XmlReadMode)

使用指定的文件和 DataSet 将 XML 架构和数据读入 XmlReadMode

ReadXml(Stream, XmlReadMode)

使用指定的 DataSetStream 将 XML 架构和数据读入 XmlReadMode

ReadXml(TextReader, XmlReadMode)

使用指定的 DataSetTextReader 将 XML 架构和数据读入 XmlReadMode

ReadXml(String)

使用指定的文件将 XML 架构和数据读入 DataSet

ReadXml(TextReader)

使用指定的 TextReader 将 XML 架构和数据读入 DataSet

ReadXml(Stream)

使用指定的 Stream 将 XML 架构和数据读入 DataSet

ReadXml(XmlReader)

使用指定的 XmlReader 将 XML 架构和数据读入 DataSet

ReadXml(XmlReader, XmlReadMode)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的 DataSetXmlReader 将 XML 架构和数据读入 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);
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

mode
XmlReadMode

XmlReadMode 值之一。

返回

用于读取数据的 XmlReadMode

注解

方法 ReadXml 提供一种方法,用于仅将数据和架构从 XML 文档读取或同时读取到 中 DataSet ,而 ReadXmlSchema 该方法仅读取架构。 若要读取数据和架构,请使用包含 mode 参数的重载之ReadXML一,并将其值设置为 ReadSchema

请注意, WriteXmlWriteXmlSchema 方法的这一点也不同。 若要从 写入 XML 数据或同时写入架构和数据 DataSet,请使用 WriteXml 方法。 若要仅编写架构,请使用 WriteXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的DataRow列类型实现了 并且未实现 IDynamicMetaObjectProviderIXmlSerializable,则将引发 。

如果指定了内联架构,则内联架构用于在加载数据之前扩展现有关系结构。 例如,如果 (存在任何冲突,则会引发使用不同数据类型定义的同一表中的同一列) 异常。

如果未指定内联架构,则根据需要根据 XML 文档的结构通过推理扩展关系结构。 如果无法通过推理扩展架构以公开所有数据,则会引发异常。

注意

DataSet在序列化的 XML 中转义 (“_”) 等法定 XML 字符时, 不会将 XML 元素与其相应的 DataColumnDataTable 相关联。 本身 DataSet 仅转义 XML 元素名称中的非法 XML 字符,因此只能使用相同字符。 当对 XML 元素名称中的合法字符进行转义时,在处理时将忽略该元素。

如果 的 DataSet XML 架构包含 targetNamespace,则可能无法读取数据,并且调用 ReadXml 以加载 DataSet 包含没有限定命名空间的元素的 XML 时可能会遇到异常。 若要读取非限定元素,请在 XML 架构中将 设置为 elementFormDefault 等于“qualified”,如以下示例所示。

<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架构包含同名但类型不同的元素,则在同一命名空间中,则尝试通过指定 XmlReadMode.ReadSchema将架构读入 DataSetReadXml 时,将引发异常。 如果使用 .NET Framework版本 1.0,则不会发生此异常。

另请参阅

适用于

ReadXml(String, XmlReadMode)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的文件和 DataSet 将 XML 架构和数据读入 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

作为读取源的文件名(包括路径)。

mode
XmlReadMode

XmlReadMode 值之一。

返回

用于读取数据的 XmlReadMode

例外

注解

方法 ReadXml 提供一种方法,用于仅将数据和架构从 XML 文档读取或同时读取到 中 DataSet ,而 ReadXmlSchema 该方法仅读取架构。 若要读取数据和架构,请使用包含 mode 参数的重载之ReadXML一,并将其值设置为 ReadSchema

请注意, WriteXmlWriteXmlSchema 方法的这一点也不同。 若要从 写入 XML 数据或同时写入架构和数据 DataSet,请使用 WriteXml 方法。 若要仅编写架构,请使用 WriteXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的DataRow列类型实现了 并且未实现 IDynamicMetaObjectProviderIXmlSerializable,则将引发 。

如果指定了内联架构,则内联架构用于在加载数据之前扩展现有关系结构。 例如,如果 (存在任何冲突,则会引发使用不同数据类型定义的同一表中的同一列) 异常。

如果未指定内联架构,则根据需要根据 XML 文档的结构通过推理扩展关系结构。 如果无法通过推理扩展架构以公开所有数据,则会引发异常。

注意

DataSet在序列化的 XML 中转义 (“_”) 等法定 XML 字符时, 不会将 XML 元素与其相应的 DataColumnDataTable 相关联。 本身 DataSet 仅转义 XML 元素名称中的非法 XML 字符,因此只能使用相同字符。 当对 XML 元素名称中的合法字符进行转义时,在处理时将忽略该元素。

如果 的 DataSet XML 架构包含 targetNamespace,则可能无法读取数据,并且调用 ReadXml 以加载 DataSet 包含没有限定命名空间的元素的 XML 时可能会遇到异常。 若要读取非限定元素,请在 XML 架构中将 设置为 elementFormDefault 等于“qualified”,如以下示例所示。

<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架构包含同名但类型不同的元素,则在同一命名空间中,则尝试通过指定 XmlReadMode.ReadSchema将架构读入 DataSetReadXml 时,将引发异常。 如果使用 .NET Framework版本 1.0,则不会发生此异常。

另请参阅

适用于

ReadXml(Stream, XmlReadMode)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的 DataSetStream 将 XML 架构和数据读入 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);
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
Public Function ReadXml (stream As Stream, mode As XmlReadMode) As XmlReadMode

参数

stream
Stream

从中读取的 Stream

mode
XmlReadMode

XmlReadMode 值之一。

返回

用于读取数据的 XmlReadMode

注解

方法 ReadXml 提供一种方法,用于仅将数据和架构从 XML 文档读取或同时读取到 中 DataSet ,而 ReadXmlSchema 该方法仅读取架构。 若要读取数据和架构,请使用包含 mode 参数的重载之ReadXML一,并将其值设置为 ReadSchema

这同样适用于 WriteXmlWriteXmlSchema 方法。 若要从 写入 XML 数据或同时写入架构和数据 DataSet,请使用 WriteXml 方法。 若要仅编写架构,请使用 WriteXmlSchema 方法。

注意

使用 ReadXml 并将 设置为 XmlReadModeDiffgram时,目标 DataSet 的内容和原始 DataSet 内容可能因差异图的生成和处理方式而异。 有关 diffgram 的详细信息,请参阅 DiffGrams

注意

InvalidOperationException如果从 中读取或写入的 中的DataRow列类型实现了 并且未实现 IDynamicMetaObjectProviderIXmlSerializable,则将引发 。

如果指定了内联架构,则内联架构用于在加载数据之前扩展现有关系结构。 例如,如果 (存在任何冲突,则会引发使用不同数据类型定义的同一表中的同一列) 异常。

如果未指定内联架构,则根据需要根据 XML 文档的结构通过推理扩展关系结构。 如果无法通过推理扩展架构以公开所有数据,则会引发异常。

注意

DataSet在序列化的 XML 中转义 (“_”) 等法定 XML 字符时, 不会将 XML 元素与其相应的 DataColumnDataTable 相关联。 本身 DataSet 仅转义 XML 元素名称中的非法 XML 字符,因此只能使用相同字符。 当对 XML 元素名称中的合法字符进行转义时,在处理时将忽略该元素。

如果 的 DataSet XML 架构包含 targetNamespace,则可能无法读取数据,并且您在调用 ReadXml 以加载 DataSet 包含没有限定命名空间的元素的 时可能会遇到异常。 若要读取非限定元素,请在 XML 架构中将 设置为 elementFormDefault 等于“qualified”,如以下示例所示。

<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架构包含同名但类型不同的元素,则在同一命名空间中,则尝试通过指定 XmlReadMode.ReadSchema将架构读入 DataSetReadXml 时,将引发异常。 如果使用 .NET Framework版本 1.0,则不会发生此异常。

另请参阅

适用于

ReadXml(TextReader, XmlReadMode)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的 DataSetTextReader 将 XML 架构和数据读入 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);
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

mode
XmlReadMode

XmlReadMode 值之一。

返回

用于读取数据的 XmlReadMode

注解

方法 ReadXml 提供了一种仅读取数据或将数据和架构从 XML 文档读取到 DataSet 中的方法,而 ReadXmlSchema 该方法仅读取架构。 若要读取数据和架构,请使用包含 mode 参数的重载之ReadXML一,并将其值设置为 ReadSchema

请注意,对于 和 WriteXmlSchema 方法,分别也是如此WriteXml。 若要从 写入 XML 数据,或者同时写入 DataSet架构和数据,请使用 WriteXml 方法。 若要仅编写架构,请使用 WriteXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的列类型DataRow实现IDynamicMetaObjectProvider并且不实现 IXmlSerializable,则会引发 。

如果指定了内联架构,则内联架构用于在加载数据之前扩展现有关系结构。 例如,如果 (存在任何冲突,则会引发异常) 同一表中使用不同数据类型定义的同一列。

如果未指定内联架构,则根据需要根据 XML 文档的结构通过推理扩展关系结构。 如果无法通过推理扩展架构以公开所有数据,则会引发异常。

注意

DataSet在序列化 XML 中转义 (“_”) 等法定 XML 字符时,不会将 XML 元素与其相应的 DataColumnDataTable 关联。 本身 DataSet 仅转义 XML 元素名称中的非法 XML 字符,因此只能使用相同的字符。 当对 XML 元素名称中的法定字符进行转义时,在处理时将忽略该元素。

如果 的 DataSet XML 架构包含 targetNamespace,则可能无法读取数据,并且调用 ReadXml 以使用包含没有限定命名空间的元素的 加载 DataSet 时可能会遇到异常。 若要读取非限定元素,请在 XML 架构中将 等于“限定”, elementFormDefault 如以下示例所示。

<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包含同一命名空间中名称相同但类型不同的元素,则在尝试通过指定 XmlReadMode.ReadSchema将架构读取到 时DataSetReadXml,将引发异常。 如果使用 .NET Framework版本 1.0,则不会发生此异常。

另请参阅

适用于

ReadXml(String)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的文件将 XML 架构和数据读入 DataSet

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

作为读取源的文件名(包括路径)。

返回

用于读取数据的 XmlReadMode

例外

示例

以下示例首先创建一个包含一DataTable个 、两列和十行的简单 DataSet 。 架构 DataSet 和数据通过调用 WriteXml 方法写入磁盘。 创建第二 DataSet 个 , ReadXml 并使用 方法填充架构和数据。

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

注解

方法 ReadXml 提供了一种仅读取数据或将数据和架构从 XML 文档读取到 DataSet 中的方法,而 ReadXmlSchema 该方法仅读取架构。 若要读取数据和架构,请使用包含 mode 参数的重载之ReadXML一,并将其值设置为 ReadSchema

请注意,对于 和 WriteXmlSchema 方法,分别也是如此WriteXml。 若要从 写入 XML 数据,或者同时写入 DataSet架构和数据,请使用 WriteXml 方法。 若要仅编写架构,请使用 WriteXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的列类型DataRow实现IDynamicMetaObjectProvider并且不实现 IXmlSerializable,则会引发 。

如果指定了内联架构,则内联架构用于在加载数据之前扩展现有关系结构。 例如,如果 (存在任何冲突,则会引发异常) 同一表中使用不同数据类型定义的同一列。

如果未指定内联架构,则根据需要根据 XML 文档的结构通过推理扩展关系结构。 如果无法通过推理扩展架构以公开所有数据,则会引发异常。

注意

DataSet在序列化 XML 中转义 (“_”) 等法定 XML 字符时,不会将 XML 元素与其相应的 DataColumnDataTable 关联。 本身 DataSet 仅转义 XML 元素名称中的非法 XML 字符,因此只能使用相同的字符。 当对 XML 元素名称中的法定字符进行转义时,在处理时将忽略该元素。

如果 的 DataSet XML 架构包含 targetNamespace,则可能无法读取数据,并且调用 ReadXml 以加载 DataSet 包含没有限定命名空间的元素的 XML 时可能会遇到异常。 若要读取非限定元素,请在 XML 架构中将 等于“限定”, elementFormDefault 如以下示例所示。

<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包含同一命名空间中名称相同但类型不同的元素,则在尝试通过指定 XmlReadMode.ReadSchema将架构读取到 时DataSetReadXml,将引发异常。 如果使用 .NET Framework版本 1.0,则不会发生此异常。

另请参阅

适用于

ReadXml(TextReader)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的 TextReader 将 XML 架构和数据读入 DataSet

public:
 System::Data::XmlReadMode ReadXml(System::IO::TextReader ^ reader);
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

返回

用于读取数据的 XmlReadMode

示例

以下示例首先创建一个包含一DataTable个 、两列和十行的简单 DataSet 。 架构 DataSet 和数据通过调用 WriteXml 方法写入磁盘。 创建第二 DataSet 个 , ReadXml 并使用 方法填充架构和数据。

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

注解

方法 ReadXml 提供了一种仅读取数据或将数据和架构从 XML 文档读取到 DataSet 中的方法,而 ReadXmlSchema 该方法仅读取架构。 若要读取数据和架构,请使用包含 mode 参数的重载之ReadXML一,并将其值设置为 ReadSchema

请注意,对于 和 WriteXmlSchema 方法,分别也是如此WriteXml。 若要从 写入 XML 数据,或者同时写入 DataSet架构和数据,请使用 WriteXml 方法。 若要仅编写架构,请使用 WriteXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的列类型DataRow实现IDynamicMetaObjectProvider并且不实现 IXmlSerializable,则会引发 。

如果指定了内联架构,则内联架构用于在加载数据之前扩展现有关系结构。 例如,如果 (存在任何冲突,则会引发异常) 同一表中使用不同数据类型定义的同一列。

如果未指定内联架构,则根据需要根据 XML 文档的结构通过推理扩展关系结构。 如果无法通过推理扩展架构以公开所有数据,则会引发异常。

注意

DataSet在序列化 XML 中转义 (“_”) 等法定 XML 字符时,不会将 XML 元素与其相应的 DataColumnDataTable 关联。 本身 DataSet 仅转义 XML 元素名称中的非法 XML 字符,因此只能使用相同的字符。 当对 XML 元素名称中的法定字符进行转义时,在处理时将忽略该元素。

如果 的 DataSet XML 架构包含 targetNamespace,则可能无法读取数据,并且调用 ReadXml 以使用包含没有限定命名空间的元素的 加载 DataSet 时可能会遇到异常。 若要读取非限定元素,请在 XML 架构中将 等于“限定”, elementFormDefault 如以下示例所示。

<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 类包括 StreamReaderStringReader 类。

注意

如果架构DataSet包含同一命名空间中名称相同但类型不同的元素,则在尝试通过指定 XmlReadMode.ReadSchema将架构读取到 时DataSetReadXml,将引发异常。 如果使用 .NET Framework版本 1.0,则不会发生此异常。

另请参阅

适用于

ReadXml(Stream)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的 Stream 将 XML 架构和数据读入 DataSet

public:
 System::Data::XmlReadMode ReadXml(System::IO::Stream ^ 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
Public Function ReadXml (stream As Stream) As XmlReadMode

参数

stream
Stream

Stream 派生的对象。

返回

用于读取数据的 XmlReadMode

示例

以下示例首先创建一个包含一DataTable个 、两列和十行的简单 DataSet 。 架构 DataSet 和数据通过调用 WriteXml 方法写入磁盘。 创建第二 DataSet 个 , ReadXml 并使用 方法填充架构和数据。

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

注解

方法 ReadXml 提供了一种仅读取数据或将数据和架构从 XML 文档读取到 DataSet 中的方法,而 ReadXmlSchema 该方法仅读取架构。 若要读取数据和架构,请使用包含 mode 参数的重载之ReadXML一,并将其值设置为 ReadSchema

请注意,对于 和 WriteXmlSchema 方法,分别也是如此WriteXml。 若要从 写入 XML 数据,或者同时写入 DataSet架构和数据,请使用 WriteXml 方法。 若要仅编写架构,请使用 WriteXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的列类型DataRow实现IDynamicMetaObjectProvider并且不实现 IXmlSerializable,则会引发 。

如果指定了内联架构,则内联架构用于在加载数据之前扩展现有关系结构。 例如,如果 (存在任何冲突,则会引发异常) 同一表中使用不同数据类型定义的同一列。

如果未指定内联架构,则根据需要根据 XML 文档的结构通过推理扩展关系结构。 如果无法通过推理扩展架构以公开所有数据,则会引发异常。

注意

DataSet在序列化 XML 中转义 (“_”) 等法定 XML 字符时,不会将 XML 元素与其相应的 DataColumnDataTable 关联。 本身 DataSet 仅转义 XML 元素名称中的非法 XML 字符,因此只能使用相同的字符。 当对 XML 元素名称中的法定字符进行转义时,在处理时将忽略该元素。

如果 的 DataSet XML 架构包含 targetNamespace,则可能无法读取数据,并且调用 ReadXml 以使用包含没有限定命名空间的元素的 加载 DataSet 时可能会遇到异常。 若要读取非限定元素,请在 XML 架构中将 设置为 elementFormDefault 等于“qualified”,如以下示例所示。

<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架构包含同名但类型不同的元素,则在同一命名空间中,则尝试通过指定 XmlReadMode.ReadSchema将架构读入 DataSetReadXml 时,将引发异常。 如果使用 .NET Framework版本 1.0,则不会发生此异常。

另请参阅

适用于

ReadXml(XmlReader)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

使用指定的 XmlReader 将 XML 架构和数据读入 DataSet

public:
 System::Data::XmlReadMode ReadXml(System::Xml::XmlReader ^ reader);
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

返回

用于读取数据的 XmlReadMode

示例

以下示例首先创建一个包含一DataTable个 、两列和十行的简单 DataSet 。 架构 DataSet 和数据通过调用 WriteXml 方法写入磁盘。 创建第二 DataSet 个,并使用 ReadXml 方法填充架构和数据。

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

注解

方法 ReadXml 提供一种方法,用于仅将数据和架构从 XML 文档读取或同时读取到 中 DataSet ,而 ReadXmlSchema 该方法仅读取架构。 若要读取数据和架构,请使用包含 mode 参数的重载之ReadXML一,并将其值设置为 ReadSchema

请注意, WriteXmlWriteXmlSchema 方法的这一点也不同。 若要从 写入 XML 数据或同时写入架构和数据 DataSet,请使用 WriteXml 方法。 若要仅编写架构,请使用 WriteXmlSchema 方法。

注意

InvalidOperationException如果从 中读取或写入的 中的DataRow列类型实现了 并且未实现 IDynamicMetaObjectProviderIXmlSerializable,则将引发 。

如果指定了内联架构,则内联架构用于在加载数据之前扩展现有关系结构。 例如,如果 (存在任何冲突,则会引发使用不同数据类型定义的同一表中的同一列) 异常。

如果未指定内联架构,则根据需要根据 XML 文档的结构通过推理扩展关系结构。 如果无法通过推理扩展架构以公开所有数据,则会引发异常。

注意

DataSet在序列化的 XML 中转义 (“_”) 等法定 XML 字符时, 不会将 XML 元素与其相应的 DataColumnDataTable 相关联。 本身 DataSet 仅转义 XML 元素名称中的非法 XML 字符,因此只能使用相同字符。 当对 XML 元素名称中的合法字符进行转义时,在处理时将忽略该元素。

如果 的 DataSet XML 架构包含 targetNamespace,则可能无法读取数据,并且调用 ReadXml 以加载 DataSet 包含没有限定命名空间的元素的 XML 时可能会遇到异常。 若要读取非限定元素,请在 XML 架构中将 设置为 elementFormDefault 等于“qualified”,如以下示例所示。

<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.XmlTextReaderXmlReader 继承。

注意

如果 的DataSet架构包含同名但类型不同的元素,则在同一命名空间中,则尝试通过指定 XmlReadMode.ReadSchema将架构读入 DataSetReadXml 时,将引发异常。 如果使用 .NET Framework版本 1.0,则不会发生此异常。

另请参阅

适用于