DataTable.ReadXml DataTable.ReadXml DataTable.ReadXml DataTable.ReadXml Method

定義

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

多載

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

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

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

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

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

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

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

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

備註

方法提供一種方式,只會DataTableReadXmlSchema XML 檔讀取資料,或將資料和架構寫入其中,而方法只會讀取架構。 ReadXmlThe 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包含參數的多載,並將其值設定ReadSchema為。To read both data and schema, use one of the ReadXML overloads that include the XmlReadMode parameter, and set its value to ReadSchema.

請注意, WriteXmlWriteXmlSchema方法的相同也是如此。Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要從DataTable寫入 XML 資料或架構和資料,請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.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn 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) ReadXml(Stream) 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);
member this.ReadXml : System.IO.Stream -> System.Data.XmlReadMode

參數

stream
Stream Stream Stream Stream

衍生自 Stream 的物件An object that derives from Stream

傳回

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

範例

下列範例會建立包含DataTable兩個數據行和10個數據列的。The following example creates a DataTable containing two columns and ten rows. 此範例會叫DataTableWriteXml方法,將架構和資料寫入記憶體資料流程。The 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的和其子代都會以所提供Stream之中的資料載入。The 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.

方法提供一種方式,只會DataTableReadXmlSchema XML 檔讀取資料,或將資料和架構寫入其中,而方法只會讀取架構。 ReadXmlThe 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.

請注意, WriteXmlWriteXmlSchema方法的相同也是如此。Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要從DataTable寫入 XML 資料或架構和資料,請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.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn 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) ReadXml(TextReader) ReadXml(TextReader) 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);
member this.ReadXml : System.IO.TextReader -> System.Data.XmlReadMode
Public Function ReadXml (reader As TextReader) As XmlReadMode

參數

reader
TextReader TextReader TextReader TextReader

TextReader,將用來讀取資料。The TextReader that will be used to read the data.

傳回

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

範例

下列範例會建立包含DataTable兩個數據行和10個數據列的。The following example creates a DataTable containing two columns and ten rows. 此範例會叫DataTableWriteXml方法,將架構和資料寫入記憶體資料流程。The 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的和其子代都會以所提供TextReader之中的資料載入。The 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.

方法提供一種方式,只會DataTableReadXmlSchema XML 檔讀取資料,或將資料和架構寫入其中,而方法只會讀取架構。 ReadXmlThe 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.

請注意, WriteXmlWriteXmlSchema方法的相同也是如此。Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要從DataTable寫入 XML 資料或架構和資料,請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.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn 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) ReadXml(String) ReadXml(String) 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 String String String

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

傳回

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 String的中,會使用名為之檔案中的資料,載入目前及其子代。The 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.

方法提供一種方式,只會DataTableReadXmlSchema XML 檔讀取資料,或將資料和架構寫入其中,而方法只會讀取架構。 ReadXmlThe 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.

請注意, WriteXmlWriteXmlSchema方法的相同也是如此。Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要從DataTable寫入 XML 資料或架構和資料,請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.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn 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) ReadXml(XmlReader) ReadXml(XmlReader) 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);
member this.ReadXml : System.Xml.XmlReader -> System.Data.XmlReadMode
Public Function ReadXml (reader As XmlReader) As XmlReadMode

參數

reader
XmlReader XmlReader XmlReader XmlReader

XmlReader,將用來讀取資料。The XmlReader that will be used to read the data.

傳回

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

範例

下列範例會建立包含DataTable兩個數據行和10個數據列的。The following example creates a DataTable containing two columns and ten rows. 這個範例會將DataTable架構和資料XmlReader寫入。The 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 XmlReader的中,會使用名為之檔案中的資料,載入目前及其子代。The 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.

方法提供一種方式,只會DataTableReadXmlSchema XML 檔讀取資料,或將資料和架構寫入其中,而方法只會讀取架構。 ReadXmlThe 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.

請注意, WriteXmlWriteXmlSchema方法的相同也是如此。Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. 若要從DataTable寫入 XML 資料或架構和資料,請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.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn 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.

另請參閱

適用於