DataTable.ReadXml Method (TextReader)

Reads XML schema and data into the DataTable using the specified TextReader.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

Public Function ReadXml ( _
	reader As TextReader _
) As XmlReadMode


Type: System.IO.TextReader

The TextReader that will be used to read the data.

Return Value

Type: System.Data.XmlReadMode
The XmlReadMode used to read the data.

The current DataTable and its descendents are loaded with the data from the supplied TextReader. 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.

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.

Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. To write XML data, or both schema and data from the DataTable, use the WriteXml method. To write just the schema, use the WriteXmlSchema method.


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.

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.

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.


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. The DataSet itself only escapes illegal XML characters in XML element names and hence can only consume the same. When legal characters in XML element name are escaped, the element is ignored while processing.

The following example creates a DataTable containing two columns and ten rows. The example writes the DataTable schema and data to a memory stream, by invoking the WriteXml method. The example creates a second DataTable and calls the ReadXml method to fill it with schema and data.

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

  ' 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

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

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

  Return table
End Function 

Private Sub PrintValues(ByVal table As DataTable, _
  ByVal label As String)
  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
  Next row
End Sub

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft