DataTable.ReadXml 方法

定义

将 XML 架构和数据读入 DataTable

重载

ReadXml(Stream)

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

ReadXml(TextReader)

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

ReadXml(String)

从指定的文件将 XML 架构和数据读入 DataTable

ReadXml(XmlReader)

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

注解

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

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

注意

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

ReadXml(Stream)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

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

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 两列和十行的 。 该示例通过调用 WriteXml 方法将DataTable架构和数据写入内存流。 该示例创建第二 DataTable 个 ,并调用 ReadXml 方法以使用架构和数据填充它。

private static void DemonstrateReadWriteXMLDocumentWithStream()
{
    DataTable table = CreateTestTable("XmlDemo");
    PrintValues(table, "Original table");

    // Write the schema and data to XML in a memory stream.
    System.IO.MemoryStream xmlStream = new System.IO.MemoryStream();
    table.WriteXml(xmlStream, XmlWriteMode.WriteSchema);

    // Rewind the memory stream.
    xmlStream.Position = 0;

    DataTable newTable = new DataTable();
    newTable.ReadXml(xmlStream);

    // Print out values in the table.
    PrintValues(newTable, "New table");
}

private static DataTable CreateTestTable(string tableName)
{
    // Create a test DataTable with two columns and a few rows.
    DataTable table = new DataTable(tableName);
    DataColumn column = new DataColumn("id", typeof(System.Int32));
    column.AutoIncrement = true;
    table.Columns.Add(column);

    column = new DataColumn("item", typeof(System.String));
    table.Columns.Add(column);

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

    table.AcceptChanges();
    return table;
}

private static void PrintValues(DataTable table, string label)
{
    // Display the contents of the supplied DataTable:
    Console.WriteLine(label);
    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn column in table.Columns)
        {
            Console.Write("\t{0}", row[column]);
        }
        Console.WriteLine();
    }
}
Private Sub DemonstrateReadWriteXMLDocumentWithStream()
  Dim table As DataTable = CreateTestTable("XmlDemo")
  PrintValues(table, "Original table")

  ' Write the schema and data to XML in a memory stream.
  Dim xmlStream As New System.IO.MemoryStream()
  table.WriteXml(xmlStream, XmlWriteMode.WriteSchema)

  ' Rewind the memory stream.
  xmlStream.Position = 0

  Dim newTable As New DataTable
  newTable.ReadXml(xmlStream)

  ' Print out values in the table.
  PrintValues(newTable, "New Table")
End Sub

Private Function CreateTestTable(ByVal tableName As String) As DataTable
  ' Create a test DataTable with two columns and a few rows.
  Dim table As New DataTable(tableName)
  Dim column As New DataColumn("id", GetType(System.Int32))
  column.AutoIncrement = True
  table.Columns.Add(column)

  column = New DataColumn("item", GetType(System.String))
  table.Columns.Add(column)

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

  table.AcceptChanges()
  Return table
End Function

Private Sub PrintValues(ByVal table As DataTable, ByVal label As String)
  ' Display the contents of the supplied DataTable:
  Console.WriteLine(label)
  For Each row As DataRow In table.Rows
    For Each column As DataColumn In table.Columns
      Console.Write("{0}{1}", ControlChars.Tab, row(column))
    Next column
    Console.WriteLine()
  Next row
End Sub

注解

当前 DataTable 及其后代使用提供 Stream的数据加载。 此方法的行为与 方法的行为相同 DataSet.ReadXml ,只不过在本例中,仅针对当前表及其后代加载数据。

方法 ReadXml 提供一种方法,用于仅将数据和架构从 XML 文档读取到 中 DataTable ,而 ReadXmlSchema 该方法仅读取架构。

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

注意

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

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

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

注意

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

另请参阅

适用于

ReadXml(TextReader)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

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

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 两列和十行的 。 该示例通过调用 WriteXml 方法将DataTable架构和数据写入内存流。 该示例创建第二 DataTable 个 ,并调用 ReadXml 方法以使用架构和数据填充它。

private static void DemonstrateReadWriteXMLDocumentWithReader()
{
    DataTable table = CreateTestTable("XmlDemo");
    PrintValues(table, "Original table");

    // Write the schema and data to XML in a memory stream.
    System.IO.MemoryStream xmlStream = new System.IO.MemoryStream();
    table.WriteXml(xmlStream, XmlWriteMode.WriteSchema);

    // Rewind the memory stream.
    xmlStream.Position = 0;

    System.IO.StreamReader reader =
        new System.IO.StreamReader(xmlStream);
    DataTable newTable = new DataTable();
    newTable.ReadXml(reader);

    // Print out values in the table.
    PrintValues(newTable, "New table");
}

private static DataTable CreateTestTable(string tableName)
{
    // Create a test DataTable with two columns and a few rows.
    DataTable table = new DataTable(tableName);
    DataColumn column = new DataColumn("id", typeof(System.Int32));
    column.AutoIncrement = true;
    table.Columns.Add(column);

    column = new DataColumn("item", typeof(System.String));
    table.Columns.Add(column);

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

    table.AcceptChanges();
    return table;
}

private static void PrintValues(DataTable table, string label)
{
    Console.WriteLine(label);
    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn column in table.Columns)
        {
            Console.Write("\t{0}", row[column]);
        }
        Console.WriteLine();
    }
}
Private Sub DemonstrateReadWriteXMLDocumentWithReader()
  Dim table As DataTable = CreateTestTable("XmlDemo")
  PrintValues(table, "Original table")

  ' Write the schema and data to XML in a memory stream.
  Dim xmlStream As New System.IO.MemoryStream()
  table.WriteXml(xmlStream, XmlWriteMode.WriteSchema)

  ' Rewind the memory stream.
  xmlStream.Position = 0

  Dim reader As New System.IO.StreamReader(xmlStream)
  Dim newTable As New DataTable
  newTable.ReadXml(reader)

  ' Print out values in the table.
  PrintValues(newTable, "New Table")
End Sub

Private Function CreateTestTable(ByVal tableName As String) _
  As DataTable
  ' Create a test DataTable with two columns and a few rows.
  Dim table As New DataTable(tableName)
  Dim column As New DataColumn("id", GetType(System.Int32))
  column.AutoIncrement = True
  table.Columns.Add(column)

  column = New DataColumn("item", GetType(System.String))
  table.Columns.Add(column)

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

  table.AcceptChanges()
  Return table
End Function

Private Sub PrintValues(ByVal table As DataTable, _
  ByVal label As String)
  Console.WriteLine(label)
  For Each row As DataRow In table.Rows
    For Each column As DataColumn In table.Columns
      Console.Write("{0}{1}", ControlChars.Tab, row(column))
    Next column
    Console.WriteLine()
  Next row
End Sub

注解

当前 DataTable 及其后代使用提供 TextReader的数据加载。 此方法的行为与 方法的行为相同 DataSet.ReadXml ,只不过在本例中,仅针对当前表及其后代加载数据。

方法 ReadXml 提供一种方法,用于仅将数据和架构从 XML 文档读取到 中 DataTable ,而 ReadXmlSchema 该方法仅读取架构。

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

注意

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

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

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

备注

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

另请参阅

适用于

ReadXml(String)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

从指定的文件将 XML 架构和数据读入 DataTable

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 两列和十行的 。 该示例将 DataTable 架构和数据写入磁盘。 该示例创建第二 DataTable 个 ,并调用 ReadXml 方法以使用架构和数据填充它。

private static void DemonstrateReadWriteXMLDocumentWithString()
{
    DataTable table = CreateTestTable("XmlDemo");
    PrintValues(table, "Original table");

    string fileName = "C:\\TestData.xml";
    table.WriteXml(fileName, XmlWriteMode.WriteSchema);

    DataTable newTable = new DataTable();
    newTable.ReadXml(fileName);

    // Print out values in the table.
    PrintValues(newTable, "New table");
}

private static DataTable CreateTestTable(string tableName)
{
    // Create a test DataTable with two columns and a few rows.
    DataTable table = new DataTable(tableName);
    DataColumn column = new DataColumn("id", typeof(System.Int32));
    column.AutoIncrement = true;
    table.Columns.Add(column);

    column = new DataColumn("item", typeof(System.String));
    table.Columns.Add(column);

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

    table.AcceptChanges();
    return table;
}

private static void PrintValues(DataTable table, string label)
{
    Console.WriteLine(label);
    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn column in table.Columns)
        {
            Console.Write("\t{0}", row[column]);
        }
        Console.WriteLine();
    }
}
Private Sub DemonstrateReadWriteXMLDocumentWithString()
  Dim table As DataTable = CreateTestTable("XmlDemo")
  PrintValues(table, "Original table")

  ' Write the schema and data to XML in a file.
  Dim fileName As String = "C:\TestData.xml"
  table.WriteXml(fileName, XmlWriteMode.WriteSchema)

  Dim newTable As New DataTable
  newTable.ReadXml(fileName)

  ' Print out values in the table.
  PrintValues(newTable, "New Table")
End Sub

Private Function CreateTestTable(ByVal tableName As String) _
  As DataTable

  ' Create a test DataTable with two columns and a few rows.
  Dim table As New DataTable(tableName)
  Dim column As New DataColumn("id", GetType(System.Int32))
  column.AutoIncrement = True
  table.Columns.Add(column)

  column = New DataColumn("item", GetType(System.String))
  table.Columns.Add(column)

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

  table.AcceptChanges()
  Return table
End Function

Private Sub PrintValues(ByVal table As DataTable, _
  ByVal label As String)
  Console.WriteLine(label)
  For Each row As DataRow In table.Rows
    For Each column As DataColumn In table.Columns
      Console.Write("{0}{1}", ControlChars.Tab, row(column))
    Next column
    Console.WriteLine()
  Next row
End Sub

注解

当前 DataTable 及其后代使用提供的 String中名为 的文件中的数据加载。 此方法的行为与 方法的行为相同 DataSet.ReadXml ,只不过在本例中,仅针对当前表及其后代加载数据。

方法 ReadXml 提供一种方法,用于仅将数据和架构从 XML 文档读取到 中 DataTable ,而 ReadXmlSchema 该方法仅读取架构。

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

备注

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

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

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

备注

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

using System.Data;
public class A {
   static void Main(string[] args) {
      DataTable tabl = new DataTable("mytable");
      tabl.Columns.Add(new DataColumn("id", typeof(int)));
      for (int i = 0; i < 10; i++) {
         DataRow row = tabl.NewRow();
         row["id"] = i;
         tabl.Rows.Add(row);
      }
      tabl.WriteXml("f.xml", XmlWriteMode.WriteSchema);
      DataTable newt = new DataTable();
      newt.ReadXml("f.xml");
   }
}

另请参阅

适用于

ReadXml(XmlReader)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

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

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 两列和十行的 。 该示例将DataTable架构和数据写入 。XmlReader 该示例创建第二 DataTable 个 ,并调用 ReadXml 方法以使用 实例中的 XmlReader 架构和数据填充它。

private static void DemonstrateReadWriteXMLDocumentWithReader()
{
    DataTable table = CreateTestTable("XmlDemo");
    PrintValues(table, "Original table");

    // Write the schema and data to XML in a memory stream.
    System.IO.MemoryStream xmlStream = new System.IO.MemoryStream();
    table.WriteXml(xmlStream, XmlWriteMode.WriteSchema);

    // Rewind the memory stream.
    xmlStream.Position = 0;

    System.Xml.XmlTextReader reader =
        new System.Xml.XmlTextReader(xmlStream);
    DataTable newTable = new DataTable();
    newTable.ReadXml(reader);

    // Print out values in the table.
    PrintValues(newTable, "New table");
}

private static DataTable CreateTestTable(string tableName)
{
    // Create a test DataTable with two columns and a few rows.
    DataTable table = new DataTable(tableName);
    DataColumn column = new DataColumn("id", typeof(System.Int32));
    column.AutoIncrement = true;
    table.Columns.Add(column);

    column = new DataColumn("item", typeof(System.String));
    table.Columns.Add(column);

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

    table.AcceptChanges();
    return table;
}

private static void PrintValues(DataTable table, string label)
{
    Console.WriteLine(label);
    foreach (DataRow row in table.Rows)
    {
        foreach (DataColumn column in table.Columns)
        {
            Console.Write("\t{0}", row[column]);
        }
        Console.WriteLine();
    }
}
Private Sub DemonstrateReadWriteXMLDocumentWithReader()
  Dim table As DataTable = CreateTestTable("XmlDemo")
  PrintValues(table, "Original table")

  ' Write the schema and data to XML in a memory stream.
  Dim xmlStream As New System.IO.MemoryStream()
  table.WriteXml(xmlStream, XmlWriteMode.WriteSchema)

  ' Rewind the memory stream.
  xmlStream.Position = 0

  Dim reader As New System.Xml.XmlTextReader(xmlStream)
  Dim newTable As New DataTable
  newTable.ReadXml(reader)

  ' Print out values in the table.
  PrintValues(newTable, "New Table")
End Sub

Private Function CreateTestTable(ByVal tableName As String) _
  As DataTable
  ' Create a test DataTable with two columns and a few rows.
  Dim table As New DataTable(tableName)
  Dim column As New DataColumn("id", GetType(System.Int32))
  column.AutoIncrement = True
  table.Columns.Add(column)

  column = New DataColumn("item", GetType(System.String))
  table.Columns.Add(column)

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

  table.AcceptChanges()
  Return table
End Function

Private Sub PrintValues(ByVal table As DataTable, _
  ByVal label As String)
  Console.WriteLine(label)
  For Each row As DataRow In table.Rows
    For Each column As DataColumn In table.Columns
      Console.Write("{0}{1}", ControlChars.Tab, row(column))
    Next column
    Console.WriteLine()
  Next row
End Sub

注解

当前 DataTable 及其后代使用提供的 XmlReader中名为 的文件中的数据加载。 此方法的行为与 方法的行为相同 ReadXml ,只不过在本例中,仅针对当前表及其后代加载数据。

方法 ReadXml 提供一种方法,用于仅将数据和架构从 XML 文档读取到 中 DataTable ,而 ReadXmlSchema 该方法仅读取架构。

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

备注

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

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

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

备注

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

另请参阅

适用于