DataTable.ReadXml Method (String)


Reads XML schema and data into the DataTable from the specified file.

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

member ReadXml : 
        fileName:string -> XmlReadMode


Type: System.String

The name of the file from which 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 file named in the supplied String. 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.

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.WriteXml("f.xml", XmlWriteMode.WriteSchema);
      DataTable newt = new DataTable();

The following example creates a DataTable containing two columns and ten rows. The example writes the DataTable schema and data to disk. The example creates a second DataTable and calls the ReadXml method to fill it with schema and data.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0
Return to top