DataTable.ReadXml 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將 XML 結構與資料讀取到 DataTable.
多載
| 名稱 | Description |
|---|---|
| ReadXml(Stream) | |
| ReadXml(TextReader) |
利用指定的 TextReaderXML 結構與資料讀取。DataTable |
| ReadXml(String) |
從指定的檔案讀取 XML 架構和資料。DataTable |
| ReadXml(XmlReader) |
備註
此 ReadXml 方法提供一種僅讀取資料,或同時讀取資料與結構的資料 DataTable ,從 XML 文件中讀取的方法,而該 ReadXmlSchema 方法僅讀取結構。 要同時讀取資料與結構,請使用包含參數的XmlReadMode超載,ReadXML並將其值設為 ReadSchema。
請注意,與WriteXmlSchema方法分別也是WriteXml同理。 若要撰寫 XML 資料,或同時撰寫結構與資料 DataTable,請使用該 WriteXml 方法。 如果只寫結構,就用這個 WriteXmlSchema 方法。
Note
InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable。
ReadXml(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
參數
傳回
他們 XmlReadMode 會讀取資料。
範例
以下範例會產生包含兩欄十列的 。DataTable 範例是透過 DataTable 呼叫 WriteXml 該方法,將結構和資料寫入記憶體串流。 範例中會建立第二個 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 方法提供一種僅讀取資料,或同時讀取資料與結構的資料 DataTable ,從 XML 文件中讀取的方法,而該 ReadXmlSchema 方法僅讀取結構。
請注意,與WriteXmlSchema方法分別也是WriteXml同理。 若要撰寫 XML 資料,或同時撰寫結構與資料 DataTable,請使用該 WriteXml 方法。 如果只寫結構,就用這個 WriteXmlSchema 方法。
Note
InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable。
若指定內聯結構,則在載入資料前,內行結構用於擴充現有關聯結構。 若存在衝突(例如同一資料表中同一欄位以不同資料型態定義),會提出例外。
若未指定內列結構,則根據 XML 文件的結構,透過推理擴展關聯結構。 若無法透過推理擴展結構以暴露所有資料,則會提出例外。
Note
當DataSet序列化 XML 中合法的 XML 字元如「_」逃逸時,也不會將 XML 元素與其對應DataColumnDataTable元素關聯。 本身 DataSet 只能逃逸 XML 元素名稱中的非法 XML 字元,因此只能消耗相同的資料。 當 XML 元素名稱中的法律字元被逃逸時,該元素在處理過程中會被忽略。
另請參閱
適用於
ReadXml(TextReader)
利用指定的 TextReaderXML 結構與資料讀取。DataTable
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 範例是透過 DataTable 呼叫 WriteXml 該方法,將結構和資料寫入記憶體串流。 範例中會建立第二個 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 方法提供一種僅讀取資料,或同時讀取資料與結構的資料 DataTable ,從 XML 文件中讀取的方法,而該 ReadXmlSchema 方法僅讀取結構。
請注意,與WriteXmlSchema方法分別也是WriteXml同理。 若要撰寫 XML 資料,或同時撰寫結構與資料 DataTable,請使用該 WriteXml 方法。 如果只寫結構,就用這個 WriteXmlSchema 方法。
Note
InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable。
若指定內聯結構,則在載入資料前,內行結構用於擴充現有關聯結構。 若存在衝突(例如同一資料表中同一欄位以不同資料型態定義),會提出例外。
若未指定內列結構,則根據 XML 文件的結構,透過推理擴展關聯結構。 若無法透過推理擴展結構以暴露所有資料,則會提出例外。
Note
當DataSet序列化 XML 中合法的 XML 字元如「_」逃逸時,也不會將 XML 元素與其對應DataColumnDataTable元素關聯。 本身 DataSet 只能逃逸 XML 元素名稱中的非法 XML 字元,因此只能消耗相同的資料。 當 XML 元素名稱中的法律字元被逃逸時,該元素在處理過程中會被忽略。
另請參閱
適用於
ReadXml(String)
從指定的檔案讀取 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 方法提供一種僅讀取資料,或同時讀取資料與結構的資料 DataTable ,從 XML 文件中讀取的方法,而該 ReadXmlSchema 方法僅讀取結構。
請注意,與WriteXmlSchema方法分別也是WriteXml同理。 若要撰寫 XML 資料,或同時撰寫結構與資料 DataTable,請使用該 WriteXml 方法。 如果只寫結構,就用這個 WriteXmlSchema 方法。
Note
InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable。
若指定內聯結構,則在載入資料前,內行結構用於擴充現有關聯結構。 若存在衝突(例如同一資料表中同一欄位以不同資料型態定義),會提出例外。
若未指定內列結構,則根據 XML 文件的結構,透過推理擴展關聯結構。 若無法透過推理擴展結構以暴露所有資料,則會提出例外。
Note
當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)
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
參數
傳回
他們 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 方法提供一種僅讀取資料,或同時讀取資料與結構的資料 DataTable ,從 XML 文件中讀取的方法,而該 ReadXmlSchema 方法僅讀取結構。
請注意,與WriteXmlSchema方法分別也是WriteXml同理。 若要撰寫 XML 資料,或同時撰寫結構與資料 DataTable,請使用該 WriteXml 方法。 如果只寫結構,就用這個 WriteXmlSchema 方法。
Note
InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable。
若指定內聯結構,則在載入資料前,內行結構用於擴充現有關聯結構。 若存在衝突(例如同一資料表中同一欄位以不同資料型態定義),會提出例外。
若未指定內列結構,則根據 XML 文件的結構,透過推理擴展關聯結構。 若無法透過推理擴展結構以暴露所有資料,則會提出例外。
Note
當DataSet序列化 XML 中合法的 XML 字元如「_」逃逸時,也不會將 XML 元素與其對應DataColumnDataTable元素關聯。 本身 DataSet 只能逃逸 XML 元素名稱中的非法 XML 字元,因此只能消耗相同的資料。 當 XML 元素名稱中的法律字元被逃逸時,該元素在處理過程中會被忽略。