DataTable.ReadXmlSchema Method

Definition

Reads an XML schema into the DataTable.

Overloads

ReadXmlSchema(XmlReader)

Reads an XML schema into the DataTable using the specified XmlReader.

ReadXmlSchema(String)

Reads an XML schema into the DataTable from the specified file.

ReadXmlSchema(TextReader)

Reads an XML schema into the DataTable using the specified TextReader.

ReadXmlSchema(Stream)

Reads an XML schema into the DataTable using the specified stream.

Remarks

Use the ReadXmlSchema method to create the schema for a DataTable. The schema includes table, relation, and constraint definitions.

To write a schema to an XML document, use the WriteXmlSchema method.

The XML schema is interpreted according to the XSD standard.

The ReadXmlSchema method is generally invoked before invoking the ReadXml method which is used to fill the DataTable.

ReadXmlSchema(XmlReader)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Reads an XML schema into the DataTable using the specified XmlReader.

C#
public void ReadXmlSchema(System.Xml.XmlReader? reader);
C#
public void ReadXmlSchema(System.Xml.XmlReader reader);

Parameters

reader
XmlReader

The XmlReader used to read the schema information.

Examples

The following console application creates a new DataTable, and writes the schema for that table to a MemoryStream. Then, the example creates a new DataTable and reads its schema from the saved XML schema, using a XmlTextReader (which inherits from XmlReader) as its source.

C#
private static void DemonstrateReadWriteXMLSchemaWithReader()
{
    DataTable table = CreateTestTable("XmlDemo");
    PrintSchema(table, "Original table");

    // Write the schema to XML in a memory stream.
    System.IO.MemoryStream xmlStream =
        new System.IO.MemoryStream();
    table.WriteXmlSchema(xmlStream);

    // Rewind the memory stream.
    xmlStream.Position = 0;

    DataTable newTable = new DataTable();
    System.Xml.XmlTextReader reader =
        new System.Xml.XmlTextReader(xmlStream);
    newTable.ReadXmlSchema(reader);

    // Print out values in the table.
    PrintSchema(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 PrintSchema(DataTable table, string label)
{
    // Display the schema of the supplied DataTable:
    Console.WriteLine(label);
    foreach (DataColumn column in table.Columns)
    {
        Console.WriteLine("\t{0}: {1}", column.ColumnName,
            column.DataType.Name);
    }
    Console.WriteLine();
}

Remarks

Use the ReadXmlSchema method to create the schema for a DataTable. The schema includes table, relation, and constraint definitions.

To write a schema to an XML document, use the WriteXmlSchema method.

The XML schema is interpreted according to the XSD standard.

Data corruption can occur if the msdata:DataType and the xs:type types do not match. No exception will be thrown.

The ReadXmlSchema method is generally invoked before invoking the ReadXml method which is used to fill the DataTable.

Note

The way to create a nested relation using XML schema is to have implicit nested elements. Additionally, the nested relation could be re-wired, to use explicit column names. It is mandatory for elements to be implicitly nested for the corresponding DataTables to participate in a nested relation.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ReadXmlSchema(String)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Reads an XML schema into the DataTable from the specified file.

C#
public void ReadXmlSchema(string fileName);

Parameters

fileName
String

The name of the file from which to read the schema information.

Examples

The following console application creates a new DataTable, and writes the schema for that table to a file. Then, the example creates a new DataTable and reads its schema from the saved XML schema, using the file as its source.

C#
private static void DemonstrateReadWriteXMLSchemaWithFile()
{
    DataTable table = CreateTestTable("XmlDemo");
    PrintSchema(table, "Original table");

    // Write the schema to XML in a file.
    string xmlFile = "C:\\SchemaDemo.xml";
    table.WriteXmlSchema(xmlFile);

    DataTable newTable = new DataTable();
    newTable.ReadXmlSchema(xmlFile);

    // Print out values in the table.
    PrintSchema(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 PrintSchema(DataTable table, string label)
{
    // Display the schema of the supplied DataTable:
    Console.WriteLine(label);
    foreach (DataColumn column in table.Columns)
    {
        Console.WriteLine("\t{0}: {1}", column.ColumnName,
            column.DataType.Name);
    }
    Console.WriteLine();
}

Remarks

Use the ReadXmlSchema method to create the schema for a DataTable. The schema includes table, relation, and constraint definitions.

To write a schema to an XML document, use the WriteXmlSchema method.

The XML schema is interpreted according to the XSD standard.

Data corruption can occur if the msdata:DataType and the xs:type types do not match. No exception will be thrown.

The ReadXmlSchema method is generally invoked before invoking the ReadXml method which is used to fill the DataTable.

To create a nested relation using XML schema, use implicit nested elements. You can also reconfigure the nested relation to use explicit column names. Elements must be implicitly nested in order for the corresponding DataTables to participate in a nested relation.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ReadXmlSchema(TextReader)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Reads an XML schema into the DataTable using the specified TextReader.

C#
public void ReadXmlSchema(System.IO.TextReader? reader);
C#
public void ReadXmlSchema(System.IO.TextReader reader);

Parameters

reader
TextReader

The TextReader used to read the schema information.

Examples

The following console application creates a new DataTable, and writes the schema for that table to a MemoryStream. Then, the example creates a new DataTable and reads its schema from the saved XML schema, using a StreamReader (which inherits from TextReader) as its source.

C#
private static void DemonstrateReadWriteXMLSchemaWithReader()
{
    DataTable table = CreateTestTable("XmlDemo");
    PrintSchema(table, "Original table");

    // Write the schema to XML in a memory stream.
    System.IO.MemoryStream xmlStream = new System.IO.MemoryStream();
    table.WriteXmlSchema(xmlStream);

    // Rewind the memory stream.
    xmlStream.Position = 0;

    DataTable newTable = new DataTable();
    System.IO.StreamReader reader =
        new System.IO.StreamReader(xmlStream);
    newTable.ReadXmlSchema(reader);

    // Print out values in the table.
    PrintSchema(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 PrintSchema(DataTable table, string label)
{
    // Display the schema of the supplied DataTable:
    Console.WriteLine(label);
    foreach (DataColumn column in table.Columns)
    {
        Console.WriteLine("\t{0}: {1}",
            column.ColumnName, column.DataType.Name);
    }
    Console.WriteLine();
}

Remarks

Use the ReadXmlSchema method to create the schema for a DataTable. The schema includes table, relation, and constraint definitions.

To write a schema to an XML document, use the WriteXmlSchema method.

The XML schema is interpreted according to the XSD standard.

Data corruption can occur if the msdata:DataType and the xs:type types do not match. No exception will be thrown.

The ReadXmlSchema method is generally invoked before invoking the ReadXml method which is used to fill the DataTable.

To create a nested relation using XML schema, use implicit nested elements. You can also reconfigure the nested relation to use explicit column names. Elements must be implicitly nested in order for the corresponding DataTables to participate in a nested relation.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ReadXmlSchema(Stream)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Reads an XML schema into the DataTable using the specified stream.

C#
public void ReadXmlSchema(System.IO.Stream? stream);
C#
public void ReadXmlSchema(System.IO.Stream stream);

Parameters

stream
Stream

The stream used to read the schema.

Examples

The following console application creates a new DataTable, and writes the schema for that table to a MemoryStream. Then, the example creates a new DataTable and reads its schema from the saved XML schema.

C#
private static void DemonstrateReadWriteXMLSchemaWithStream()
{
    DataTable table = CreateTestTable("XmlDemo");
    PrintSchema(table, "Original table");

    // Write the schema to XML in a memory stream.
    System.IO.MemoryStream xmlStream = new System.IO.MemoryStream();
    table.WriteXmlSchema(xmlStream);

    // Rewind the memory stream.
    xmlStream.Position = 0;

    DataTable newTable = new DataTable();
    newTable.ReadXmlSchema(xmlStream);

    // Print out values in the table.
    PrintSchema(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 PrintSchema(DataTable table, string label)
{
    // Display the schema of the supplied DataTable:
    Console.WriteLine(label);
    foreach (DataColumn column in table.Columns)
    {
        Console.WriteLine("\t{0}: {1}", column.ColumnName,
            column.DataType.Name);
    }
    Console.WriteLine();
}

Remarks

Use the ReadXmlSchema method to create the schema for a DataTable. The schema includes table, relation, and constraint definitions.

To write a schema to an XML document, use the WriteXmlSchema method.

The XML schema is interpreted according to the XSD standard.

Data corruption can occur if the msdata:DataType and the xs:type types do not match. No exception will be thrown.

The ReadXmlSchema method is generally invoked before invoking the ReadXml method which is used to fill the DataTable.

To create a nested relation using XML schema, use implicit nested elements. You can also configure the nested relation to use explicit column names. Elements must be implicitly nested in order for the corresponding DataTables to participate in a nested relation.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1