DataTable.ReadXmlSchema 方法

定义

将 XML 架构读入 DataTable

重载

ReadXmlSchema(XmlReader)

使用指定的 XmlReader 将 XML 架构读入 DataTable

ReadXmlSchema(String)

从指定的文件将 XML 架构读入 DataTable

ReadXmlSchema(TextReader)

使用指定的 TextReader 将 XML 架构读入 DataTable

ReadXmlSchema(Stream)

使用指定的流将 XML 架构读入 DataTable

注解

ReadXmlSchema使用 方法为 DataTable创建架构。 架构包括表、关系和约束定义。

若要将架构写入 XML 文档,请使用 WriteXmlSchema 方法。

XML 架构根据 XSD 标准进行解释。

ReadXmlSchema 调用 ReadXml 用于填充 DataTable的方法之前,通常会调用 方法。

ReadXmlSchema(XmlReader)

使用指定的 XmlReader 将 XML 架构读入 DataTable

public:
 void ReadXmlSchema(System::Xml::XmlReader ^ reader);
public void ReadXmlSchema (System.Xml.XmlReader? reader);
public void ReadXmlSchema (System.Xml.XmlReader reader);
member this.ReadXmlSchema : System.Xml.XmlReader -> unit
Public Sub ReadXmlSchema (reader As XmlReader)

参数

reader
XmlReader

用于读取架构信息的 XmlReader

示例

以下控制台应用程序创建一个新的 DataTable,并将该表的架构写入 到 。MemoryStream 然后,该示例使用 DataTableXmlTextReader 继承自 XmlReader) 作为源的 (创建新的并从保存的 XML 架构读取其架构。

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();
}
Private Sub DemonstrateReadWriteXMLSchemaWithReader()
  Dim table As DataTable = CreateTestTable("XmlDemo")
  PrintSchema(table, "Original table")

  ' Write the schema to XML in a memory stream.
  Dim xmlStream As New System.IO.MemoryStream()
  table.WriteXmlSchema(xmlStream)

  ' Rewind the memory stream.
  xmlStream.Position = 0

  Dim newTable As New DataTable
  Dim reader As New System.Xml.XmlTextReader(xmlStream)
  newTable.ReadXmlSchema(reader)

  ' 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

注解

ReadXmlSchema使用 方法为 DataTable创建架构。 架构包括表、关系和约束定义。

若要将架构写入 XML 文档,请使用 WriteXmlSchema 方法。

XML 架构根据 XSD 标准进行解释。

如果 msdata:DataType 和 xs:type 类型不匹配,则可能发生数据损坏。 不会引发异常。

ReadXmlSchema 调用 ReadXml 用于填充 DataTable的方法之前,通常会调用 方法。

注意

使用 XML 架构创建嵌套关系的方式是具有隐式嵌套元素。 此外,嵌套关系可以重新连接,以使用显式列名。 对于相应的 DataTable,必须隐式嵌套元素才能参与嵌套关系。

另请参阅

适用于

ReadXmlSchema(String)

从指定的文件将 XML 架构读入 DataTable

public:
 void ReadXmlSchema(System::String ^ fileName);
public void ReadXmlSchema (string fileName);
member this.ReadXmlSchema : string -> unit
Public Sub ReadXmlSchema (fileName As String)

参数

fileName
String

从中读取架构信息的文件的名称。

示例

以下控制台应用程序创建一个新的 DataTable,并将该表的架构写入文件。 然后,该示例创建一个新的 DataTable ,并使用 文件作为其源从保存的 XML 架构读取其架构。

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();
}
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

注解

ReadXmlSchema使用 方法为 DataTable创建架构。 架构包括表、关系和约束定义。

若要将架构写入 XML 文档,请使用 WriteXmlSchema 方法。

XML 架构根据 XSD 标准进行解释。

如果 msdata:DataType 和 xs:type 类型不匹配,则可能发生数据损坏。 不会引发异常。

ReadXmlSchema 调用 ReadXml 用于填充 DataTable的方法之前,通常会调用 方法。

若要使用 XML 架构创建嵌套关系,请使用隐式嵌套元素。 还可以将嵌套关系重新配置为使用显式列名。 元素必须隐式嵌套,以便相应的 DataTable 参与嵌套关系。

另请参阅

适用于

ReadXmlSchema(TextReader)

使用指定的 TextReader 将 XML 架构读入 DataTable

public:
 void ReadXmlSchema(System::IO::TextReader ^ reader);
public void ReadXmlSchema (System.IO.TextReader? reader);
public void ReadXmlSchema (System.IO.TextReader reader);
member this.ReadXmlSchema : System.IO.TextReader -> unit
Public Sub ReadXmlSchema (reader As TextReader)

参数

reader
TextReader

用于读取架构信息的 TextReader

示例

以下控制台应用程序创建一个新的 DataTable,并将该表的架构写入 到 。MemoryStream 然后,该示例使用 DataTableStreamReader 继承自 TextReader) 作为源的 (创建新的并从保存的 XML 架构读取其架构。

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();
}
Private Sub DemonstrateReadWriteXMLSchemaWithReader()
  Dim table As DataTable = CreateTestTable("XmlDemo")
  PrintSchema(table, "Original table")

  ' Write the schema to XML in a memory stream.
  Dim xmlStream As New System.IO.MemoryStream()
  table.WriteXmlSchema(xmlStream)

  ' Rewind the memory stream.
  xmlStream.Position = 0

  Dim newTable As New DataTable
  Dim reader As New System.IO.StreamReader(xmlStream)
  newTable.ReadXmlSchema(reader)

  ' 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

注解

ReadXmlSchema使用 方法为 DataTable创建架构。 架构包括表、关系和约束定义。

若要将架构写入 XML 文档,请使用 WriteXmlSchema 方法。

XML 架构根据 XSD 标准进行解释。

如果 msdata:DataType 和 xs:type 类型不匹配,则可能发生数据损坏。 不会引发异常。

ReadXmlSchema 调用 ReadXml 用于填充 DataTable的方法之前,通常会调用 方法。

若要使用 XML 架构创建嵌套关系,请使用隐式嵌套元素。 还可以将嵌套关系重新配置为使用显式列名。 元素必须隐式嵌套,以便相应的 DataTable 参与嵌套关系。

另请参阅

适用于

ReadXmlSchema(Stream)

使用指定的流将 XML 架构读入 DataTable

public:
 void ReadXmlSchema(System::IO::Stream ^ stream);
public void ReadXmlSchema (System.IO.Stream? stream);
public void ReadXmlSchema (System.IO.Stream stream);
member this.ReadXmlSchema : System.IO.Stream -> unit
Public Sub ReadXmlSchema (stream As Stream)

参数

stream
Stream

用于读取架构的流。

示例

以下控制台应用程序创建一个新的 DataTable,并将该表的架构写入 到 。MemoryStream 然后,该示例创建一个新的 DataTable ,并从保存的 XML 架构中读取其架构。

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();
}
Private Sub DemonstrateReadWriteXMLSchemaWithStream()
  Dim table As DataTable = CreateTestTable("XmlDemo")
  PrintSchema(table, "Original table")

  ' Write the schema to XML in a memory stream.
  Dim xmlStream As New System.IO.MemoryStream()
  table.WriteXmlSchema(xmlStream)

  ' Rewind the memory stream.
  xmlStream.Position = 0

  Dim newTable As New DataTable
  newTable.ReadXmlSchema(xmlStream)

  ' 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

注解

ReadXmlSchema使用 方法为 DataTable创建架构。 架构包括表、关系和约束定义。

若要将架构写入 XML 文档,请使用 WriteXmlSchema 方法。

XML 架构根据 XSD 标准进行解释。

如果 msdata:DataType 和 xs:type 类型不匹配,则可能发生数据损坏。 不会引发异常。

ReadXmlSchema 调用 ReadXml 用于填充 DataTable的方法之前,通常会调用 方法。

若要使用 XML 架构创建嵌套关系,请使用隐式嵌套元素。 还可以将嵌套关系配置为使用显式列名。 元素必须隐式嵌套,以便相应的 DataTable 参与嵌套关系。

另请参阅

适用于