Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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 XmlReadMode ReadXml (
	TextReader reader
)
public XmlReadMode ReadXml (
	TextReader reader
)
public function ReadXml (
	reader : TextReader
) : XmlReadMode
Not applicable.

Parameters

reader

The TextReader that will be used to read the data.

Return Value

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. To read both data and schema, use one of the ReadXML overloads that includes the XmlReadMode parameter, and set its value to ReadSchema.

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.

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.

NoteNote:

The DataSet does not associate an XML elementwith 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 DataSet 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 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();
    }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft