DataTable.ReadXml Yöntem
Tanım
Aşırı Yüklemeler
ReadXml(Stream) |
XML şemasını ve verilerini, DataTable belirtilen kullanarak öğesine okur Stream .Reads XML schema and data into the DataTable using the specified Stream. |
ReadXml(TextReader) |
XML şemasını ve verilerini, DataTable belirtilen kullanarak öğesine okur TextReader .Reads XML schema and data into the DataTable using the specified TextReader. |
ReadXml(String) |
Belirtilen dosyadan XML şemasını ve verilerini okur DataTable .Reads XML schema and data into the DataTable from the specified file. |
ReadXml(XmlReader) |
XML şemasını ve verilerini, DataTable belirtilen kullanarak öğesine okur XmlReader .Reads XML Schema and Data into the DataTable using the specified XmlReader. |
Açıklamalar
ReadXmlYöntemi BIR XML belgesinden yalnızca verileri veya veri ve şemayı okumak için bir yol sağlar DataTable , ancak ReadXmlSchema Yöntem yalnızca şemayı okur.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. Hem verileri hem de şemayı okumak için ReadXML
parametresini içeren aşırı yüklemelerden birini kullanın XmlReadMode
ve değerini olarak ayarlayın ReadSchema
.To read both data and schema, use one of the ReadXML
overloads that include the XmlReadMode
parameter, and set its value to ReadSchema
.
Aynı şekilde, ve yöntemleri için sırasıyla doğru olduğunu unutmayın WriteXml WriteXmlSchema .Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. XML verileri veya ' den şema ve veri yazmak için DataTable
WriteXml
yöntemini kullanın.To write XML data, or both schema and data from the DataTable
, use the WriteXml
method. Yalnızca şemayı yazmak için WriteXmlSchema
yöntemini kullanın.To write just the schema, use the WriteXmlSchema
method.
Not
InvalidOperationException DataRow
Okunmakta olan veya yazılan bir sütun türü uygularsa IDynamicMetaObjectProvider ve uygularsa, bir oluşturulur IXmlSerializable .An 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)
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
Parametreler
Döndürülenler
XmlReadModeVerileri okumak için kullanılır.The XmlReadMode used to read the data.
Örnekler
Aşağıdaki örnek, DataTable iki sütun ve on satır içeren bir oluşturur.The following example creates a DataTable containing two columns and ten rows. Örnek, DataTable yöntemi çağırarak şemayı ve verileri bir bellek akışına yazar WriteXml .The example writes the DataTable schema and data to a memory stream, by invoking the WriteXml method. Örnek bir ikinci oluşturur DataTable ve ReadXml bunu şema ve verilerle birlikte dolduracak şekilde çağırır.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
Açıklamalar
Geçerli DataTable ve alt öğelerinden biri, sağlanan verilerle birlikte yüklenir Stream .The current DataTable and its descendents are loaded with the data from the supplied Stream. Bu yöntemin davranışı, yöntemi ile aynıdır DataSet.ReadXml , ancak bu durumda veriler yalnızca geçerli tablo ve alt öğeleri için yüklenir.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.
ReadXmlYöntemi BIR XML belgesinden yalnızca verileri veya veri ve şemayı okumak için bir yol sağlar DataTable , ancak ReadXmlSchema Yöntem yalnızca şemayı okur.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.
Aynı şekilde, ve yöntemleri için sırasıyla doğru olduğunu unutmayın WriteXml WriteXmlSchema .Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. XML verileri veya ' den şema ve veri yazmak için DataTable
WriteXml
yöntemini kullanın.To write XML data, or both schema and data from the DataTable
, use the WriteXml
method. Yalnızca şemayı yazmak için WriteXmlSchema
yöntemini kullanın.To write just the schema, use the WriteXmlSchema
method.
Not
InvalidOperationException DataRow
Okunmakta olan veya yazılan bir sütun türü uygularsa IDynamicMetaObjectProvider ve uygularsa, bir oluşturulur IXmlSerializable .An InvalidOperationException will be thrown if a column type in the DataRow
being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.
Bir satır içi şema belirtilmezse, satır içi şema veri yükleme öncesinde varolan ilişkisel yapıyı genişletmek için kullanılır.If an in-line schema is specified, the in-line schema is used to extend the existing relational structure prior to loading the data. Çakışmalar varsa (örneğin, aynı tablodaki aynı sütun farklı veri türleriyle tanımlanmış) bir özel durum oluşturulur.If there are any conflicts (for example, the same column in the same table defined with different data types) an exception is raised.
Hiçbir satır içi şema belirtilmezse, çıkarım yoluyla ilişkisel yapı, XML belgesinin yapısına göre gerektiği gibi genişletilir.If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. Şema tüm verileri göstermek için çıkarım yoluyla genişletilemiyorsa, özel durum oluşturulur.If the schema cannot be extended through inference in order to expose all data, an exception is raised.
Not
, DataSet
BIR XML öğesini karşılık gelen DataColumn
veya DataTable
("") gibi geçerli XML karakterlerinin SERI hale getirilmiş XML 'te atılması durumunda ilişkilendirmez.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
Yalnızca XML öğe adlarında GEÇERSIZ XML karakterlerinin çıkar ve bu nedenle yalnızca aynısını kullanabilir.The DataSet
itself only escapes illegal XML characters in XML element names and hence can only consume the same. XML öğesinin adındaki geçerli karakterler çıkarıldığında, öğe işlenirken göz ardı edilir.When legal characters in XML element name are escaped, the element is ignored while processing.
Ayrıca bkz.
Şunlara uygulanır
ReadXml(TextReader)
XML şemasını ve verilerini, DataTable belirtilen kullanarak öğesine okur TextReader .Reads 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
Parametreler
- reader
- TextReader
TextReaderBu, verileri okumak için kullanılacaktır.The TextReader that will be used to read the data.
Döndürülenler
XmlReadModeVerileri okumak için kullanılır.The XmlReadMode used to read the data.
Örnekler
Aşağıdaki örnek, DataTable iki sütun ve on satır içeren bir oluşturur.The following example creates a DataTable containing two columns and ten rows. Örnek, DataTable yöntemi çağırarak şemayı ve verileri bir bellek akışına yazar WriteXml .The example writes the DataTable schema and data to a memory stream, by invoking the WriteXml method. Örnek bir ikinci oluşturur DataTable ve ReadXml bunu şema ve verilerle birlikte dolduracak şekilde çağırır.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
Açıklamalar
Geçerli DataTable ve alt öğelerinden biri, sağlanan verilerle birlikte yüklenir TextReader .The current DataTable and its descendents are loaded with the data from the supplied TextReader. Bu yöntemin davranışı, yöntemi ile aynıdır DataSet.ReadXml , ancak bu durumda veriler yalnızca geçerli tablo ve alt öğeleri için yüklenir.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.
ReadXmlYöntemi BIR XML belgesinden yalnızca verileri veya veri ve şemayı okumak için bir yol sağlar DataTable , ancak ReadXmlSchema Yöntem yalnızca şemayı okur.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.
Aynı şekilde, ve yöntemleri için sırasıyla doğru olduğunu unutmayın WriteXml WriteXmlSchema .Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. XML verileri veya ' den şema ve veri yazmak için DataTable
WriteXml
yöntemini kullanın.To write XML data, or both schema and data from the DataTable
, use the WriteXml
method. Yalnızca şemayı yazmak için WriteXmlSchema
yöntemini kullanın.To write just the schema, use the WriteXmlSchema
method.
Not
InvalidOperationException DataRow
Okunmakta olan veya yazılan bir sütun türü uygularsa IDynamicMetaObjectProvider ve uygularsa, bir oluşturulur IXmlSerializable .An InvalidOperationException will be thrown if a column type in the DataRow
being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.
Bir satır içi şema belirtilmezse, satır içi şema veri yükleme öncesinde varolan ilişkisel yapıyı genişletmek için kullanılır.If an in-line schema is specified, the in-line schema is used to extend the existing relational structure prior to loading the data. Çakışmalar varsa (örneğin, aynı tablodaki aynı sütun farklı veri türleriyle tanımlanmış) bir özel durum oluşturulur.If there are any conflicts (for example, the same column in the same table defined with different data types) an exception is raised.
Hiçbir satır içi şema belirtilmezse, çıkarım yoluyla ilişkisel yapı, XML belgesinin yapısına göre gerektiği gibi genişletilir.If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. Şema tüm verileri göstermek için çıkarım yoluyla genişletilemiyorsa, özel durum oluşturulur.If the schema cannot be extended through inference in order to expose all data, an exception is raised.
Not
, DataSet
BIR XML öğesini karşılık gelen DataColumn
veya DataTable
("") gibi geçerli XML karakterlerinin SERI hale getirilmiş XML 'te atılması durumunda ilişkilendirmez.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
Yalnızca XML öğe adlarında GEÇERSIZ XML karakterlerinin çıkar ve bu nedenle yalnızca aynısını kullanabilir.The DataSet
itself only escapes illegal XML characters in XML element names and hence can only consume the same. XML öğesinin adındaki geçerli karakterler çıkarıldığında, öğe işlenirken göz ardı edilir.When legal characters in XML element name are escaped, the element is ignored while processing.
Ayrıca bkz.
Şunlara uygulanır
ReadXml(String)
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
Parametreler
- fileName
- String
Verilerin okunacağı dosyanın adı.The name of the file from which to read the data.
Döndürülenler
XmlReadModeVerileri okumak için kullanılır.The XmlReadMode used to read the data.
Örnekler
Aşağıdaki örnek, DataTable iki sütun ve on satır içeren bir oluşturur.The following example creates a DataTable containing two columns and ten rows. Örnek, DataTable şemayı ve verileri diske yazar.The example writes the DataTable schema and data to disk. Örnek bir ikinci oluşturur DataTable ve ReadXml bunu şema ve verilerle birlikte dolduracak şekilde çağırır.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
Açıklamalar
Geçerli DataTable ve alt öğelerinden biri, sağlanan adlı dosyadaki verilerle yüklenir String .The current DataTable and its descendents are loaded with the data from the file named in the supplied String. Bu yöntemin davranışı, yöntemi ile aynıdır DataSet.ReadXml , ancak bu durumda veriler yalnızca geçerli tablo ve alt öğeleri için yüklenir.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.
ReadXmlYöntemi BIR XML belgesinden yalnızca verileri veya veri ve şemayı okumak için bir yol sağlar DataTable , ancak ReadXmlSchema Yöntem yalnızca şemayı okur.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.
Aynı şekilde, ve yöntemleri için sırasıyla doğru olduğunu unutmayın WriteXml WriteXmlSchema .Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. XML verileri veya ' den şema ve veri yazmak için DataTable
WriteXml
yöntemini kullanın.To write XML data, or both schema and data from the DataTable
, use the WriteXml
method. Yalnızca şemayı yazmak için WriteXmlSchema
yöntemini kullanın.To write just the schema, use the WriteXmlSchema
method.
Not
InvalidOperationException DataRow
Okunmakta olan veya yazılan bir sütun türü uygularsa IDynamicMetaObjectProvider ve uygularsa, bir oluşturulur IXmlSerializable .An InvalidOperationException will be thrown if a column type in the DataRow
being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.
Bir satır içi şema belirtilmezse, satır içi şema veri yükleme öncesinde varolan ilişkisel yapıyı genişletmek için kullanılır.If an in-line schema is specified, the in-line schema is used to extend the existing relational structure prior to loading the data. Çakışmalar varsa (örneğin, aynı tablodaki aynı sütun farklı veri türleriyle tanımlanmış) bir özel durum oluşturulur.If there are any conflicts (for example, the same column in the same table defined with different data types) an exception is raised.
Hiçbir satır içi şema belirtilmezse, çıkarım yoluyla ilişkisel yapı, XML belgesinin yapısına göre gerektiği gibi genişletilir.If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. Şema tüm verileri göstermek için çıkarım yoluyla genişletilemiyorsa, özel durum oluşturulur.If the schema cannot be extended through inference in order to expose all data, an exception is raised.
Not
, DataSet
BIR XML öğesini karşılık gelen DataColumn
veya DataTable
("") gibi geçerli XML karakterlerinin SERI hale getirilmiş XML 'te atılması durumunda ilişkilendirmez.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
Yalnızca XML öğe adlarında GEÇERSIZ XML karakterlerinin çıkar ve bu nedenle yalnızca aynısını kullanabilir.The DataSet
itself only escapes illegal XML characters in XML element names and hence can only consume the same. XML öğesinin adındaki geçerli karakterler çıkarıldığında, öğe işlenirken göz ardı edilir.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");
}
}
Ayrıca bkz.
Şunlara uygulanır
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
Parametreler
- reader
- XmlReader
XmlReaderBu, verileri okumak için kullanılacaktır.The XmlReader that will be used to read the data.
Döndürülenler
XmlReadModeVerileri okumak için kullanılır.The XmlReadMode used to read the data.
Örnekler
Aşağıdaki örnek, DataTable iki sütun ve on satır içeren bir oluşturur.The following example creates a DataTable containing two columns and ten rows. Örnek, DataTable şema ve verileri bir öğesine yazar XmlReader .The example writes the DataTable schema and data to an XmlReader. Örnek bir ikinci oluşturur DataTable ve ReadXml bunu, örnekten şema ve verilerle birlikte dolduracak şekilde çağırır 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
Açıklamalar
Geçerli DataTable ve alt öğelerinden biri, sağlanan adlı dosyadaki verilerle yüklenir XmlReader .The current DataTable and its descendents are loaded with the data from the file named in the supplied XmlReader. Bu yöntemin davranışı, yöntemi ile aynıdır ReadXml , ancak bu durumda veriler yalnızca geçerli tablo ve alt öğeleri için yüklenir.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.
ReadXmlYöntemi BIR XML belgesinden yalnızca verileri veya veri ve şemayı okumak için bir yol sağlar DataTable , ancak ReadXmlSchema Yöntem yalnızca şemayı okur.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.
Aynı şekilde, ve yöntemleri için sırasıyla doğru olduğunu unutmayın WriteXml WriteXmlSchema .Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. XML verileri veya ' den şema ve veri yazmak için DataTable
WriteXml
yöntemini kullanın.To write XML data, or both schema and data from the DataTable
, use the WriteXml
method. Yalnızca şemayı yazmak için WriteXmlSchema
yöntemini kullanın.To write just the schema, use the WriteXmlSchema
method.
Not
InvalidOperationException DataRow
Okunmakta olan veya yazılan bir sütun türü uygularsa IDynamicMetaObjectProvider ve uygularsa, bir oluşturulur IXmlSerializable .An InvalidOperationException will be thrown if a column type in the DataRow
being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.
Bir satır içi şema belirtilmezse, satır içi şema veri yükleme öncesinde varolan ilişkisel yapıyı genişletmek için kullanılır.If an in-line schema is specified, the in-line schema is used to extend the existing relational structure prior to loading the data. Çakışmalar varsa (örneğin, aynı tablodaki aynı sütun farklı veri türleriyle tanımlanmış) bir özel durum oluşturulur.If there are any conflicts (for example, the same column in the same table defined with different data types) an exception is raised.
Hiçbir satır içi şema belirtilmezse, çıkarım yoluyla ilişkisel yapı, XML belgesinin yapısına göre gerektiği gibi genişletilir.If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. Şema tüm verileri göstermek için çıkarım yoluyla genişletilemiyorsa, özel durum oluşturulur.If the schema cannot be extended through inference in order to expose all data, an exception is raised.
Not
, DataSet
BIR XML öğesini karşılık gelen DataColumn
veya DataTable
("") gibi geçerli XML karakterlerinin SERI hale getirilmiş XML 'te atılması durumunda ilişkilendirmez.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
Yalnızca XML öğe adlarında GEÇERSIZ XML karakterlerinin çıkar ve bu nedenle yalnızca aynısını kullanabilir.The DataSet
itself only escapes illegal XML characters in XML element names and hence can only consume the same. XML öğesinin adındaki geçerli karakterler çıkarıldığında, öğe işlenirken göz ardı edilir.When legal characters in XML element name are escaped, the element is ignored while processing.