DataTable.ReadXmlSchema Method (String)
Reads an XML schema into the DataTable from the specified file.
Assembly: System.Data (in System.Data.dll)
Parameters
- fileName
-
Type:
System.String
The name of the file from which to read the schema information.
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.
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.
Private Sub DemonstrateReadWriteXMLSchemaWithFile() Dim table As DataTable = CreateTestTable("XmlDemo") PrintSchema(table, "Original table") Dim xmlFile As String = "SchemaDemo.xml" ' Write the schema to XML. table.WriteXmlSchema(xmlFile) Dim newTable As New DataTable newTable.ReadXmlSchema(xmlFile) ' Print out values in the table. PrintSchema(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 PrintSchema(ByVal table As DataTable, _ ByVal label As String) ' Display the schema of the supplied DataTable: Console.WriteLine(label) For Each column As DataColumn In table.Columns Console.WriteLine("{0}{1}: {2}", ControlChars.Tab, _ column.ColumnName, column.DataType.Name) Next column End Sub
Available since 2.0