DataTable.ReadXml 方法

定義

將 XML 結構描述和資料讀入 DataTableReads XML schema and data into the DataTable.

多載

ReadXml(Stream)

使用指定的 DataTable,將 XML 結構描述和資料讀入 StreamReads XML schema and data into the DataTable using the specified Stream.

ReadXml(TextReader)

使用指定的 DataTable,將 XML 結構描述和資料讀入 TextReaderReads XML schema and data into the DataTable using the specified TextReader.

ReadXml(String)

將 XML 結構描述和資料從指定的檔案讀入 DataTableReads XML schema and data into the DataTable from the specified file.

ReadXml(XmlReader)

使用指定的 DataTable,將 XML 結構描述和資料讀入 XmlReaderReads XML Schema and Data into the DataTable using the specified XmlReader.

備註

ReadXml方法提供一種方式,可將資料或資料和架構 DataTable 從 XML 檔讀取到中,而 ReadXmlSchema 方法只會讀取架構。The ReadXml method provides a way to read either data only, or both data and schema into a DataTable from an XML document, whereas the ReadXmlSchema method reads only the schema. 若要讀取資料和架構,請使用其中一個包含參數的多載, ReadXML XmlReadMode 並將其值設定為 ReadSchemaTo read both data and schema, use one of the ReadXML overloads that include the XmlReadMode parameter, and set its value to ReadSchema.

請注意,和方法也是如此 WriteXml WriteXmlSchemaNote that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要從寫入 XML 資料或的架構和資料 DataTable ,請使用 WriteXml 方法。To write XML data, or both schema and data from the DataTable, use the WriteXml method. 若只寫入架構,請使用 WriteXmlSchema 方法。To write just the schema, use the WriteXmlSchema method.

注意

InvalidOperationException如果讀取或寫入中的資料行類型 DataRowIDynamicMetaObjectProvider 執行,且未執行,則會擲回 IXmlSerializableAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

ReadXml(Stream)

使用指定的 DataTable,將 XML 結構描述和資料讀入 StreamReads XML schema and data into the DataTable using the specified Stream.

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 的物件An object that derives from Stream

傳回

XmlReadMode

XmlReadMode,用來讀取資料。The XmlReadMode used to read the data.

範例

下列範例會建立 DataTable 包含兩個數據行和10個數據列的。The following example creates a DataTable containing two columns and ten rows. 此範例會藉 DataTable 由叫用方法,將架構和資料寫入至記憶體資料流程 WriteXmlThe example writes the DataTable schema and data to a memory stream, by invoking the WriteXml method. 此範例會建立第二個 DataTable ,並呼叫 ReadXml 方法來填入架構和資料。The example creates a second DataTable and calls the ReadXml method to fill it with schema and data.

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 及其子代都會與提供的資料一起載入 StreamThe current DataTable and its descendents are loaded with the data from the supplied Stream. 這個方法的行為與方法的行為完全相同 DataSet.ReadXml ,但在此情況下,只會針對目前的資料表和其子系載入資料。The behavior of this method is identical to that of the DataSet.ReadXml method, except that in this case, data is loaded only for the current table and its descendants.

ReadXml方法提供一種方式,可將資料或資料和架構 DataTable 從 XML 檔讀取到中,而 ReadXmlSchema 方法只會讀取架構。The ReadXml method provides a way to read either data only, or both data and schema into a DataTable from an XML document, whereas the ReadXmlSchema method reads only the schema.

請注意,和方法也是如此 WriteXml WriteXmlSchemaNote that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要從寫入 XML 資料或的架構和資料 DataTable ,請使用 WriteXml 方法。To write XML data, or both schema and data from the DataTable, use the WriteXml method. 若只寫入架構,請使用 WriteXmlSchema 方法。To write just the schema, use the WriteXmlSchema method.

注意

InvalidOperationException如果讀取或寫入中的資料行類型 DataRowIDynamicMetaObjectProvider 執行,且未執行,則會擲回 IXmlSerializableAn 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 專案與其對應的關聯, DataColumn 或在 DataTable 序列化的 xml 中使用合法的 xml 字元(例如 ( "" ) )。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.

適用於

ReadXml(TextReader)

使用指定的 DataTable,將 XML 結構描述和資料讀入 TextReaderReads XML schema and data into the DataTable using the specified TextReader.

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,將用來讀取資料。The TextReader that will be used to read the data.

傳回

XmlReadMode

XmlReadMode,用來讀取資料。The XmlReadMode used to read the data.

範例

下列範例會建立 DataTable 包含兩個數據行和10個數據列的。The following example creates a DataTable containing two columns and ten rows. 此範例會藉 DataTable 由叫用方法,將架構和資料寫入至記憶體資料流程 WriteXmlThe example writes the DataTable schema and data to a memory stream, by invoking the WriteXml method. 此範例會建立第二個 DataTable ,並呼叫 ReadXml 方法來填入架構和資料。The example creates a second DataTable and calls the ReadXml method to fill it with schema and data.

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 及其子代都會與提供的資料一起載入 TextReaderThe current DataTable and its descendents are loaded with the data from the supplied TextReader. 這個方法的行為與方法的行為完全相同 DataSet.ReadXml ,但在此情況下,只會針對目前的資料表和其子系載入資料。The behavior of this method is identical to that of the DataSet.ReadXml method, except that in this case, data is loaded only for the current table and its descendants.

ReadXml方法提供一種方式,可將資料或資料和架構 DataTable 從 XML 檔讀取到中,而 ReadXmlSchema 方法只會讀取架構。The ReadXml method provides a way to read either data only, or both data and schema into a DataTable from an XML document, whereas the ReadXmlSchema method reads only the schema.

請注意,和方法也是如此 WriteXml WriteXmlSchemaNote that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要從寫入 XML 資料或的架構和資料 DataTable ,請使用 WriteXml 方法。To write XML data, or both schema and data from the DataTable, use the WriteXml method. 若只寫入架構,請使用 WriteXmlSchema 方法。To write just the schema, use the WriteXmlSchema method.

注意

InvalidOperationException如果讀取或寫入中的資料行類型 DataRowIDynamicMetaObjectProvider 執行,且未執行,則會擲回 IXmlSerializableAn 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 專案與其對應的關聯, DataColumn 或在 DataTable 序列化的 xml 中使用合法的 xml 字元(例如 ( "" ) )。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.

適用於

ReadXml(String)

將 XML 結構描述和資料從指定的檔案讀入 DataTableReads XML schema and data into the DataTable from 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

要從中讀取資料的檔案名稱。The name of the file from which to read the data.

傳回

XmlReadMode

XmlReadMode,用來讀取資料。The XmlReadMode used to read the data.

範例

下列範例會建立 DataTable 包含兩個數據行和10個數據列的。The following example creates a DataTable containing two columns and ten rows. 此範例會將 DataTable 架構和資料寫入磁片。The example writes the DataTable schema and data to disk. 此範例會建立第二個 DataTable ,並呼叫 ReadXml 方法來填入架構和資料。The example creates a second DataTable and calls the ReadXml method to fill it with schema and data.

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 和其子項會以提供的中名為之檔案的資料載入 StringThe current DataTable and its descendents are loaded with the data from the file named in the supplied String. 這個方法的行為與方法的行為完全相同 DataSet.ReadXml ,但在此情況下,只會針對目前的資料表和其子系載入資料。The behavior of this method is identical to that of the DataSet.ReadXml method, except that in this case, data is loaded only for the current table and its descendants.

ReadXml方法提供一種方式,可將資料或資料和架構 DataTable 從 XML 檔讀取到中,而 ReadXmlSchema 方法只會讀取架構。The ReadXml method provides a way to read either data only, or both data and schema into a DataTable from an XML document, whereas the ReadXmlSchema method reads only the schema.

請注意,和方法也是如此 WriteXml WriteXmlSchemaNote that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要從寫入 XML 資料或的架構和資料 DataTable ,請使用 WriteXml 方法。To write XML data, or both schema and data from the DataTable, use the WriteXml method. 若只寫入架構,請使用 WriteXmlSchema 方法。To write just the schema, use the WriteXmlSchema method.

注意

InvalidOperationException如果讀取或寫入中的資料行類型 DataRowIDynamicMetaObjectProvider 執行,且未執行,則會擲回 IXmlSerializableAn 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 專案與其對應的關聯, DataColumn 或在 DataTable 序列化的 xml 中使用合法的 xml 字元(例如 ( "" ) )。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.

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)

使用指定的 DataTable,將 XML 結構描述和資料讀入 XmlReaderReads XML Schema and Data into the DataTable using the specified XmlReader.

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,將用來讀取資料。The XmlReader that will be used to read the data.

傳回

XmlReadMode

XmlReadMode,用來讀取資料。The XmlReadMode used to read the data.

範例

下列範例會建立 DataTable 包含兩個數據行和10個數據列的。The following example creates a DataTable containing two columns and ten rows. 此範例會將 DataTable 架構和資料寫入至 XmlReaderThe example writes the DataTable schema and data to an XmlReader. 此範例會建立第二個 DataTable ,並呼叫 ReadXml 方法,以將架構和資料填入 XmlReader 實例。The example creates a second DataTable and calls the ReadXml method to fill it with schema and data from the XmlReader instance.

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 和其子項會以提供的中名為之檔案的資料載入 XmlReaderThe current DataTable and its descendents are loaded with the data from the file named in the supplied XmlReader. 這個方法的行為與方法的行為完全相同 ReadXml ,但在此情況下,只會針對目前的資料表和其子系載入資料。The behavior of this method is identical to that of the ReadXml method, except that in this case, data is loaded only for the current table and its descendants.

ReadXml方法提供一種方式,可將資料或資料和架構 DataTable 從 XML 檔讀取到中,而 ReadXmlSchema 方法只會讀取架構。The ReadXml method provides a way to read either data only, or both data and schema into a DataTable from an XML document, whereas the ReadXmlSchema method reads only the schema.

請注意,和方法也是如此 WriteXml WriteXmlSchemaNote that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要從寫入 XML 資料或的架構和資料 DataTable ,請使用 WriteXml 方法。To write XML data, or both schema and data from the DataTable, use the WriteXml method. 若只寫入架構,請使用 WriteXmlSchema 方法。To write just the schema, use the WriteXmlSchema method.

注意

InvalidOperationException如果讀取或寫入中的資料行類型 DataRowIDynamicMetaObjectProvider 執行,且未執行,則會擲回 IXmlSerializableAn 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 專案與其對應的關聯, DataColumn 或在 DataTable 序列化的 xml 中使用合法的 xml 字元(例如 ( "" ) )。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.

適用於