共用方式為


DataTable.ReadXmlSchema 方法

定義

將 XML 結構描述讀入 DataTable

多載

ReadXmlSchema(XmlReader)

使用指定的 DataTable,將 XML 結構描述讀入 XmlReader

ReadXmlSchema(String)

從指定的檔案,將 XML 結構描述讀入 DataTable

ReadXmlSchema(TextReader)

使用指定的 DataTable,將 XML 結構描述讀入 TextReader

ReadXmlSchema(Stream)

使用指定的資料流,將 XML 結構描述讀入 DataTable

備註

ReadXmlSchema使用方法來建立的DataTable架構。 架構包含數據表、關聯性和條件約束定義。

若要將架構寫入 XML 檔,請使用 WriteXmlSchema 方法。

XML 架構會根據 XSD 標準來解譯。

ReadXmlSchemaReadXml 用用來填入 DataTable的方法之前,通常會叫用 方法。

ReadXmlSchema(XmlReader)

來源:
DataTable.cs
來源:
DataTable.cs
來源:
DataTable.cs

使用指定的 DataTable,將 XML 結構描述讀入 XmlReader

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。 然後,此範例會使用繼承XmlReader自) 做為其來源的 (,XmlTextReader從儲存的 XML 架構中建立新的 DataTable 並讀取其架構。

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 類型不相符,可能會發生數據損毀。 不會擲回例外狀況。

ReadXmlSchemaReadXml 用用來填入 DataTable的方法之前,通常會叫用 方法。

注意

使用 XML 架構建立巢狀關聯的方式是具有隱含巢狀專案。 此外,巢狀關聯也可以重新有線,以使用明確的數據行名稱。 元素必須隱含巢狀化,對應的 DataTable 才能參與巢狀關聯。

另請參閱

適用於

ReadXmlSchema(String)

來源:
DataTable.cs
來源:
DataTable.cs
來源:
DataTable.cs

從指定的檔案,將 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 類型不相符,可能會發生數據損毀。 不會擲回例外狀況。

ReadXmlSchemaReadXml 用用來填入 DataTable的方法之前,通常會叫用 方法。

若要使用 XML 架構建立巢狀關聯,請使用隱含巢狀專案。 您也可以重新設定巢狀關聯,以使用明確的數據行名稱。 元素必須隱含巢狀,對應的 DataTable 才能參與巢狀關聯。

另請參閱

適用於

ReadXmlSchema(TextReader)

來源:
DataTable.cs
來源:
DataTable.cs
來源:
DataTable.cs

使用指定的 DataTable,將 XML 結構描述讀入 TextReader

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。 然後,此範例會使用繼承TextReader自) 做為其來源的 (,StreamReader從儲存的 XML 架構中建立新的 DataTable 並讀取其架構。

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 類型不相符,可能會發生數據損毀。 不會擲回例外狀況。

ReadXmlSchemaReadXml 用用來填入 DataTable的方法之前,通常會叫用 方法。

若要使用 XML 架構建立巢狀關聯,請使用隱含巢狀專案。 您也可以重新設定巢狀關聯,以使用明確的數據行名稱。 元素必須隱含巢狀,對應的 DataTable 才能參與巢狀關聯。

另請參閱

適用於

ReadXmlSchema(Stream)

來源:
DataTable.cs
來源:
DataTable.cs
來源:
DataTable.cs

使用指定的資料流,將 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 類型不相符,可能會發生數據損毀。 不會擲回例外狀況。

ReadXmlSchemaReadXml 用用來填入 DataTable的方法之前,通常會叫用 方法。

若要使用 XML 架構建立巢狀關聯,請使用隱含巢狀專案。 您也可以設定巢狀關聯,以使用明確的數據行名稱。 元素必須隱含巢狀,對應的 DataTable 才能參與巢狀關聯。

另請參閱

適用於