DataTable.WriteXml DataTable.WriteXml DataTable.WriteXml DataTable.WriteXml Method

定義

DataTable 的目前內容寫成 XML。Writes the current contents of the DataTable as XML.

多載

WriteXml(TextWriter, Boolean) WriteXml(TextWriter, Boolean) WriteXml(TextWriter, Boolean) WriteXml(TextWriter, Boolean)

使用指定的 TextWriter,將 DataTable 目前的內容寫入為 XML。Writes the current contents of the DataTable as XML using the specified TextWriter. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(TextWriter, XmlWriteMode) WriteXml(TextWriter, XmlWriteMode) WriteXml(TextWriter, XmlWriteMode) WriteXml(TextWriter, XmlWriteMode)

使用指定的 DataTableTextWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataTable using the specified TextWriter and XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema.

WriteXml(String, XmlWriteMode) WriteXml(String, XmlWriteMode) WriteXml(String, XmlWriteMode) WriteXml(String, XmlWriteMode)

使用指定的檔案和 DataTable,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataTable using the specified file and XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema.

WriteXml(Stream, XmlWriteMode) WriteXml(Stream, XmlWriteMode) WriteXml(Stream, XmlWriteMode)

使用指定的 DataTable,寫入 XmlWriteMode 的目前資料 (並選擇性寫入結構描述) 至指定的檔案。Writes the current data, and optionally the schema, for the DataTable to the specified file using the specified XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema.

WriteXml(XmlWriter, Boolean) WriteXml(XmlWriter, Boolean) WriteXml(XmlWriter, Boolean) WriteXml(XmlWriter, Boolean)

使用指定的 XmlWriter,將 DataTable 目前的內容寫入為 XML。Writes the current contents of the DataTable as XML using the specified XmlWriter.

WriteXml(XmlWriter, XmlWriteMode) WriteXml(XmlWriter, XmlWriteMode) WriteXml(XmlWriter, XmlWriteMode) WriteXml(XmlWriter, XmlWriteMode)

使用指定的 DataTableXmlWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataTable using the specified XmlWriter and XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema.

WriteXml(Stream, XmlWriteMode, Boolean) WriteXml(Stream, XmlWriteMode, Boolean) WriteXml(Stream, XmlWriteMode, Boolean)

使用指定的 DataTable,寫入 XmlWriteMode 的目前資料 (並選擇性寫入結構描述) 至指定的檔案。Writes the current data, and optionally the schema, for the DataTable to the specified file using the specified XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(TextWriter, XmlWriteMode, Boolean) WriteXml(TextWriter, XmlWriteMode, Boolean) WriteXml(TextWriter, XmlWriteMode, Boolean) WriteXml(TextWriter, XmlWriteMode, Boolean)

使用指定的 DataTableTextWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataTable using the specified TextWriter and XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(String, Boolean) WriteXml(String, Boolean) WriteXml(String, Boolean) WriteXml(String, Boolean)

使用指定的檔案,將 DataTable 目前的內容寫成 XML。Writes the current contents of the DataTable as XML using the specified file. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(Stream, Boolean) WriteXml(Stream, Boolean) WriteXml(Stream, Boolean)

使用指定的 Stream,將 DataTable 目前的內容寫入為 XML。Writes the current contents of the DataTable as XML using the specified Stream. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(XmlWriter, XmlWriteMode, Boolean) WriteXml(XmlWriter, XmlWriteMode, Boolean) WriteXml(XmlWriter, XmlWriteMode, Boolean) WriteXml(XmlWriter, XmlWriteMode, Boolean)

使用指定的 DataTableXmlWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataTable using the specified XmlWriter and XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(String) WriteXml(String) WriteXml(String) WriteXml(String)

使用指定的檔案,將 DataTable 目前的內容寫成 XML。Writes the current contents of the DataTable as XML using the specified file.

WriteXml(TextWriter) WriteXml(TextWriter) WriteXml(TextWriter) WriteXml(TextWriter)

使用指定的 TextWriter,將 DataTable 目前的內容寫入為 XML。Writes the current contents of the DataTable as XML using the specified TextWriter.

WriteXml(Stream) WriteXml(Stream) WriteXml(Stream)

使用指定的 Stream,將 DataTable 目前的內容寫入為 XML。Writes the current contents of the DataTable as XML using the specified Stream.

WriteXml(String, XmlWriteMode, Boolean) WriteXml(String, XmlWriteMode, Boolean) WriteXml(String, XmlWriteMode, Boolean) WriteXml(String, XmlWriteMode, Boolean)

使用指定的檔案和 DataTable,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataTable using the specified file and XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(XmlWriter) WriteXml(XmlWriter) WriteXml(XmlWriter) WriteXml(XmlWriter)

使用指定的 XmlWriter,將 DataTable 目前的內容寫入為 XML。Writes the current contents of the DataTable as XML using the specified XmlWriter.

範例

下列主控台應用程式會建立DataTable兩個實例, DataSet並將每個實例DataRelation新增至、建立與這兩個數據表WriteXml相關的,然後使用方法將包含在父資料表中的資料寫入至TextWriter.The following console application creates two DataTable instances, adds each to a DataSet, creates a DataRelation relating the two tables, and then uses the WriteXml method to write the data contained within the parent table to a TextWriter. 此範例會示範將writeHierarchy參數設定為其每個值時的行為。The example demonstrates the behavior when setting the writeHierarchy parameter to each of its values.

注意

這個範例示範如何使用 WriteXml 的其中一個多載版本。This example shows how to use one of the overloaded versions of WriteXml. 如需其他可能可用的範例,請參閱個別多載主題。For other examples that might be available, see the individual overload topics.

static void Main()
{
    DataSet ds = new DataSet();
    DataTable customerTable = GetCustomers();
    DataTable orderTable = GetOrders();

    ds.Tables.Add(customerTable);
    ds.Tables.Add(orderTable);
    ds.Relations.Add("CustomerOrder",
        new DataColumn[] { customerTable.Columns[0] },
        new DataColumn[] { orderTable.Columns[1] }, true);

    System.IO.StringWriter writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, false);
    PrintOutput(writer, "Customer table, without hierarchy");

    writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, true);
    PrintOutput(writer, "Customer table, with hierarchy");

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static DataTable GetCustomers()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(System.Int32));
    table.Columns.Add("Name", typeof(System.String));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    table.AcceptChanges();
    return table;
}

private static DataTable GetOrders()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create three columns; OrderID, CustomerID, and OrderDate.
    table.Columns.Add(new DataColumn("OrderID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("CustomerID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("OrderDate", typeof(System.DateTime)));

    // Set the OrderID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { table.Columns[0] };

    table.Rows.Add(new object[] { 1, 1, "12/2/2003" });
    table.Rows.Add(new object[] { 2, 1, "1/3/2004" });
    table.Rows.Add(new object[] { 3, 2, "11/13/2004" });
    table.Rows.Add(new object[] { 4, 3, "5/16/2004" });
    table.Rows.Add(new object[] { 5, 3, "5/22/2004" });
    table.Rows.Add(new object[] { 6, 4, "6/15/2004" });
    table.AcceptChanges();
    return table;
}

private static void PrintOutput(System.IO.TextWriter writer, string caption)
{
    Console.WriteLine("==============================");
    Console.WriteLine(caption);
    Console.WriteLine("==============================");
    Console.WriteLine(writer.ToString());
}
Sub Main()
  Dim ds As New DataSet
  Dim customerTable As DataTable = GetCustomers()
  Dim orderTable As DataTable = GetOrders()

  ds.Tables.Add(customerTable)
  ds.Tables.Add(orderTable)
  ds.Relations.Add("CustomerOrder", _
   New DataColumn() {customerTable.Columns(0)}, _
   New DataColumn() {orderTable.Columns(1)}, True)

  Dim writer As New System.IO.StringWriter
  customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, False)
  PrintOutput(writer, "Customer table, without hierarchy")

  writer = New System.IO.StringWriter
  customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, True)
  PrintOutput(writer, "Customer table, with hierarchy")

  Console.WriteLine("Press any key to continue.")
  Console.ReadKey()
End Sub

Private Function GetOrders() As DataTable
  ' Create sample Customers table, in order
  ' to demonstrate the behavior of the DataTableReader.
  Dim table As New DataTable

  ' Create three columns, OrderID, CustomerID, and OrderDate.
  table.Columns.Add(New DataColumn("OrderID", GetType(System.Int32)))
  table.Columns.Add(New DataColumn("CustomerID", GetType(System.Int32)))
  table.Columns.Add(New DataColumn("OrderDate", GetType(System.DateTime)))

  ' Set the OrderID column as the primary key column.
  table.PrimaryKey = New DataColumn() {table.Columns(0)}

  table.Rows.Add(New Object() {1, 1, #12/2/2003#})
  table.Rows.Add(New Object() {2, 1, #1/3/2004#})
  table.Rows.Add(New Object() {3, 2, #11/13/2004#})
  table.Rows.Add(New Object() {4, 3, #5/16/2004#})
  table.Rows.Add(New Object() {5, 3, #5/22/2004#})
  table.Rows.Add(New Object() {6, 4, #6/15/2004#})
  table.AcceptChanges()
  Return table
End Function

Private Function GetCustomers() As DataTable
  ' Create sample Customers table, in order
  ' to demonstrate the behavior of the DataTableReader.
  Dim table As New DataTable

  ' Create two columns, ID and Name.
  Dim idColumn As DataColumn = table.Columns.Add("ID", _
      GetType(System.Int32))
  table.Columns.Add("Name", GetType(System.String))

  ' Set the ID column as the primary key column.
  table.PrimaryKey = New DataColumn() {idColumn}

  table.Rows.Add(New Object() {1, "Mary"})
  table.Rows.Add(New Object() {2, "Andy"})
  table.Rows.Add(New Object() {3, "Peter"})
  table.Rows.Add(New Object() {4, "Russ"})
  table.AcceptChanges()
  Return table
End Function

Private Sub PrintOutput( _
   ByVal writer As System.IO.TextWriter, ByVal caption As String)

  Console.WriteLine("==============================")
  Console.WriteLine(caption)
  Console.WriteLine("==============================")
  Console.WriteLine(writer.ToString())
End Sub

此範例會在主控台視窗中顯示以下輸出:The example displays the following output in the console window:

==============================  
Customer table, without hierarchy  
==============================  
<NewDataSet>  
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema  
" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">  
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table1">  
      <xs:complexType>  
        <xs:choice minOccurs="0" maxOccurs="unbounded">  
          <xs:element name="Table1">  
            <xs:complexType>  
              <xs:sequence>  
                <xs:element name="ID" type="xs:int" />  
                <xs:element name="Name" type="xs:string" minOccurs="0" />  
              </xs:sequence>  
            </xs:complexType>  
          </xs:element>  
        </xs:choice>  
      </xs:complexType>  
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">  
        <xs:selector xpath=".//Table1" />  
        <xs:field xpath="ID" />  
      </xs:unique>  
    </xs:element>  
  </xs:schema>  
  <Table1>  
    <ID>1</ID>  
    <Name>Mary</Name>  
  </Table1>  
  <Table1>  
    <ID>2</ID>  
    <Name>Andy</Name>  
  </Table1>  
  <Table1>  
    <ID>3</ID>  
    <Name>Peter</Name>  
  </Table1>  
  <Table1>  
    <ID>4</ID>  
    <Name>Russ</Name>  
  </Table1>  
</NewDataSet>  
==============================  
Customer table, with hierarchy  
==============================  
<NewDataSet>  
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema  
" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">  
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table1">  
      <xs:complexType>  
        <xs:choice minOccurs="0" maxOccurs="unbounded">  
          <xs:element name="Table1">  
            <xs:complexType>  
              <xs:sequence>  
                <xs:element name="ID" type="xs:int" />  
                <xs:element name="Name" type="xs:string" minOccurs="0" />  
              </xs:sequence>  
            </xs:complexType>  
          </xs:element>  
          <xs:element name="Table2">  
            <xs:complexType>  
              <xs:sequence>  
                <xs:element name="OrderID" type="xs:int" />  
                <xs:element name="CustomerID" type="xs:int" minOccurs="0" />  
                <xs:element name="OrderDate" type="xs:dateTime" minOccurs="0" />  

              </xs:sequence>  
            </xs:complexType>  
          </xs:element>  
        </xs:choice>  
      </xs:complexType>  
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">  
        <xs:selector xpath=".//Table1" />  
        <xs:field xpath="ID" />  
      </xs:unique>  
      <xs:unique name="Table2_Constraint1" msdata:ConstraintName="Constraint1" m  
sdata:PrimaryKey="true">  
        <xs:selector xpath=".//Table2" />  
        <xs:field xpath="OrderID" />  
      </xs:unique>  
      <xs:keyref name="CustomerOrder" refer="Constraint1">  
        <xs:selector xpath=".//Table2" />  
        <xs:field xpath="CustomerID" />  
      </xs:keyref>  
    </xs:element>  
  </xs:schema>  
  <Table1>  
    <ID>1</ID>  
    <Name>Mary</Name>  
  </Table1>  
  <Table1>  
    <ID>2</ID>  
    <Name>Andy</Name>  
  </Table1>  
  <Table1>  
    <ID>3</ID>  
    <Name>Peter</Name>  
  </Table1>  
  <Table1>  
    <ID>4</ID>  
    <Name>Russ</Name>  
  </Table1>  
  <Table2>  
    <OrderID>1</OrderID>  
    <CustomerID>1</CustomerID>  
    <OrderDate>2003-12-02T00:00:00.0000000-08:00</OrderDate>  
  </Table2>  
  <Table2>  
    <OrderID>2</OrderID>  
    <CustomerID>1</CustomerID>  
    <OrderDate>2004-01-03T00:00:00.0000000-08:00</OrderDate>  
  </Table2>  
  <Table2>  
    <OrderID>3</OrderID>  
    <CustomerID>2</CustomerID>  
    <OrderDate>2004-11-13T00:00:00.0000000-08:00</OrderDate>  
  </Table2>  
  <Table2>  
    <OrderID>4</OrderID>  
    <CustomerID>3</CustomerID>  
    <OrderDate>2004-05-16T00:00:00.0000000-07:00</OrderDate>  
  </Table2>  
  <Table2>  
    <OrderID>5</OrderID>  
    <CustomerID>3</CustomerID>  
    <OrderDate>2004-05-22T00:00:00.0000000-07:00</OrderDate>  
  </Table2>  
  <Table2>  
    <OrderID>6</OrderID>  
    <CustomerID>4</CustomerID>  
    <OrderDate>2004-06-15T00:00:00.0000000-07:00</OrderDate>  
  </Table2>  
</NewDataSet>  

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that include the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

WriteXml(TextWriter, Boolean) WriteXml(TextWriter, Boolean) WriteXml(TextWriter, Boolean) WriteXml(TextWriter, Boolean)

使用指定的 TextWriter,將 DataTable 目前的內容寫入為 XML。Writes the current contents of the DataTable as XML using the specified TextWriter. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

public:
 void WriteXml(System::IO::TextWriter ^ writer, bool writeHierarchy);
public void WriteXml (System.IO.TextWriter writer, bool writeHierarchy);
member this.WriteXml : System.IO.TextWriter * bool -> unit
Public Sub WriteXml (writer As TextWriter, writeHierarchy As Boolean)

參數

writer
TextWriter TextWriter TextWriter TextWriter

用來撰寫內容的 TextWriterThe TextWriter with which to write the content.

writeHierarchy
Boolean Boolean Boolean Boolean

如果為 true,就會寫入目前資料表及它所有子代的內容。If true, write the contents of the current table and all its descendants. 如果為 false (預設值),就只會寫入目前資料表的資料。If false (the default value), write the data for the current table only.

範例

下列主控台應用程式會建立DataTable兩個實例, DataSet並將每個實例DataRelation新增至、建立與這兩個數據表WriteXml相關的,然後使用方法將包含在父資料表中的資料寫入至TextWriter.The following console application creates two DataTable instances, adds each to a DataSet, creates a DataRelation relating the two tables, and then uses the WriteXml method to write the data contained within the parent table to a TextWriter. 此範例示範將writeHierarchy參數設定為true時的行為。The example demonstrates the behavior when setting the writeHierarchy parameter to true.

static void Main()
{
    DataSet ds = new DataSet();
    DataTable customerTable = GetCustomers();
    DataTable orderTable = GetOrders();

    ds.Tables.Add(customerTable);
    ds.Tables.Add(orderTable);
    ds.Relations.Add("CustomerOrder",
        new DataColumn[] { customerTable.Columns[0] },
        new DataColumn[] { orderTable.Columns[1] }, true);

    System.IO.StringWriter writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, true);
    PrintOutput(writer, "Customer table, with hierarchy");

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static DataTable GetCustomers()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(System.Int32));
    table.Columns.Add("Name", typeof(System.String));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    table.AcceptChanges();
    return table;
}

private static DataTable GetOrders()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create three columns; OrderID, CustomerID, and OrderDate.
    table.Columns.Add(new DataColumn("OrderID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("CustomerID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("OrderDate", typeof(System.DateTime)));

    // Set the OrderID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { table.Columns[0] };

    table.Rows.Add(new object[] { 1, 1, "12/2/2003" });
    table.Rows.Add(new object[] { 2, 1, "1/3/2004" });
    table.Rows.Add(new object[] { 3, 2, "11/13/2004" });
    table.Rows.Add(new object[] { 4, 3, "5/16/2004" });
    table.Rows.Add(new object[] { 5, 3, "5/22/2004" });
    table.Rows.Add(new object[] { 6, 4, "6/15/2004" });
    table.AcceptChanges();
    return table;
}

private static void PrintOutput(System.IO.TextWriter stream, 
    string caption)
{
    Console.WriteLine("==============================");
    Console.WriteLine(caption);
    Console.WriteLine("==============================");
    Console.WriteLine(stream.ToString());
}
Sub Main()
  Dim ds As New DataSet
  Dim customerTable As DataTable = GetCustomers()
  Dim orderTable As DataTable = GetOrders()

  ds.Tables.Add(customerTable)
  ds.Tables.Add(orderTable)
  ds.Relations.Add("CustomerOrder", _
   New DataColumn() {customerTable.Columns(0)}, _
   New DataColumn() {orderTable.Columns(1)}, True)

  Dim writer As New System.IO.StringWriter
  customerTable.WriteXml(writer, True)
  PrintOutput(writer, "Customer table, with hierarchy")

  Console.WriteLine("Press any key to continue.")
  Console.ReadKey()
End Sub

Private Function GetOrders() As DataTable
  ' Create sample Customers table, in order
  ' to demonstrate the behavior of the DataTableReader.
  Dim table As New DataTable

  ' Create three columns, OrderID, CustomerID, and OrderDate.
  table.Columns.Add(New DataColumn("OrderID", GetType(System.Int32)))
  table.Columns.Add(New DataColumn("CustomerID", GetType(System.Int32)))
  table.Columns.Add(New DataColumn("OrderDate", GetType(System.DateTime)))

  ' Set the OrderID column as the primary key column.
  table.PrimaryKey = New DataColumn() {table.Columns(0)}

  table.Rows.Add(New Object() {1, 1, #12/2/2003#})
  table.Rows.Add(New Object() {2, 1, #1/3/2004#})
  table.Rows.Add(New Object() {3, 2, #11/13/2004#})
  table.Rows.Add(New Object() {4, 3, #5/16/2004#})
  table.Rows.Add(New Object() {5, 3, #5/22/2004#})
  table.Rows.Add(New Object() {6, 4, #6/15/2004#})
  table.AcceptChanges()
  Return table
End Function

Private Function GetCustomers() As DataTable
  ' Create sample Customers table, in order
  ' to demonstrate the behavior of the DataTableReader.
  Dim table As New DataTable

  ' Create two columns, ID and Name.
  Dim idColumn As DataColumn = table.Columns.Add("ID", _
      GetType(System.Int32))
  table.Columns.Add("Name", GetType(System.String))

  ' Set the ID column as the primary key column.
  table.PrimaryKey = New DataColumn() {idColumn}

  table.Rows.Add(New Object() {1, "Mary"})
  table.Rows.Add(New Object() {2, "Andy"})
  table.Rows.Add(New Object() {3, "Peter"})
  table.Rows.Add(New Object() {4, "Russ"})
  table.AcceptChanges()
  Return table
End Function

Private Sub PrintOutput( _
   ByVal stream As System.IO.TextWriter, ByVal caption As String)

  Console.WriteLine("==============================")
  Console.WriteLine(caption)
  Console.WriteLine("==============================")
  Console.WriteLine(stream.ToString())
End Sub

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that includes the XmlWriteMode parameter, and set its value to WriteSchema.

一般來說, WriteXml方法只會針對目前資料表寫入資料。Normally the WriteXml method writes the data only for the current table. 若要寫入目前資料表及其所有子系(相關資料表)的資料,請呼叫方法writeHierarchy ,並將參數設定為。 trueTo write the data for the current table and all its descendant, related tables, call the method with the writeHierarchy parameter set to true.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(TextWriter, XmlWriteMode) WriteXml(TextWriter, XmlWriteMode) WriteXml(TextWriter, XmlWriteMode) WriteXml(TextWriter, XmlWriteMode)

使用指定的 DataTableTextWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataTable using the specified TextWriter and XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema.

public:
 void WriteXml(System::IO::TextWriter ^ writer, System::Data::XmlWriteMode mode);
public void WriteXml (System.IO.TextWriter writer, System.Data.XmlWriteMode mode);
member this.WriteXml : System.IO.TextWriter * System.Data.XmlWriteMode -> unit
Public Sub WriteXml (writer As TextWriter, mode As XmlWriteMode)

參數

writer
TextWriter TextWriter TextWriter TextWriter

寫入文件所用的 TextWriterThe TextWriter used to write the document.

mode
XmlWriteMode XmlWriteMode XmlWriteMode XmlWriteMode

其中一個 XmlWriteMode 值。One of the XmlWriteMode values.

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that includes the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(String, XmlWriteMode) WriteXml(String, XmlWriteMode) WriteXml(String, XmlWriteMode) WriteXml(String, XmlWriteMode)

使用指定的檔案和 DataTable,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataTable using the specified file and XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema.

public:
 void WriteXml(System::String ^ fileName, System::Data::XmlWriteMode mode);
public void WriteXml (string fileName, System.Data.XmlWriteMode mode);
member this.WriteXml : string * System.Data.XmlWriteMode -> unit
Public Sub WriteXml (fileName As String, mode As XmlWriteMode)

參數

fileName
String String String String

資料將寫入其中的檔案名稱。The name of the file to which the data will be written.

mode
XmlWriteMode XmlWriteMode XmlWriteMode XmlWriteMode

其中一個 XmlWriteMode 值。One of the XmlWriteMode values.

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that include the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(Stream, XmlWriteMode) WriteXml(Stream, XmlWriteMode) WriteXml(Stream, XmlWriteMode)

使用指定的 DataTable,寫入 XmlWriteMode 的目前資料 (並選擇性寫入結構描述) 至指定的檔案。Writes the current data, and optionally the schema, for the DataTable to the specified file using the specified XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema.

public:
 void WriteXml(System::IO::Stream ^ stream, System::Data::XmlWriteMode mode);
public void WriteXml (System.IO.Stream stream, System.Data.XmlWriteMode mode);
member this.WriteXml : System.IO.Stream * System.Data.XmlWriteMode -> unit

參數

stream
Stream Stream Stream Stream

將資料寫入其中的資料流。The stream to which the data will be written.

mode
XmlWriteMode XmlWriteMode XmlWriteMode XmlWriteMode

其中一個 XmlWriteMode 值。One of the XmlWriteMode values.

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that includes the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(XmlWriter, Boolean) WriteXml(XmlWriter, Boolean) WriteXml(XmlWriter, Boolean) WriteXml(XmlWriter, Boolean)

使用指定的 XmlWriter,將 DataTable 目前的內容寫入為 XML。Writes the current contents of the DataTable as XML using the specified XmlWriter.

public:
 void WriteXml(System::Xml::XmlWriter ^ writer, bool writeHierarchy);
public void WriteXml (System.Xml.XmlWriter writer, bool writeHierarchy);
member this.WriteXml : System.Xml.XmlWriter * bool -> unit
Public Sub WriteXml (writer As XmlWriter, writeHierarchy As Boolean)

參數

writer
XmlWriter XmlWriter XmlWriter XmlWriter

要用來寫入內容的 XmlWriterThe XmlWriter with which to write the contents.

writeHierarchy
Boolean Boolean Boolean Boolean

如果為 true,就會寫入目前資料表及它所有子代的內容。If true, write the contents of the current table and all its descendants. 如果為 false (預設值),就只會寫入目前資料表的資料。If false (the default value), write the data for the current table only.

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that include the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

一般來說, WriteXml方法只會針對目前資料表寫入資料。Normally the WriteXml method writes the data only for the current table. 若要寫入目前資料表及其整個子代(相關資料表)的資料,請呼叫方法writeHierarchy ,並將參數設定為。 trueTo write the data for the current table and its entire descendant, related tables, call the method with the writeHierarchy parameter set to true.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(XmlWriter, XmlWriteMode) WriteXml(XmlWriter, XmlWriteMode) WriteXml(XmlWriter, XmlWriteMode) WriteXml(XmlWriter, XmlWriteMode)

使用指定的 DataTableXmlWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataTable using the specified XmlWriter and XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema.

public:
 void WriteXml(System::Xml::XmlWriter ^ writer, System::Data::XmlWriteMode mode);
public void WriteXml (System.Xml.XmlWriter writer, System.Data.XmlWriteMode mode);
member this.WriteXml : System.Xml.XmlWriter * System.Data.XmlWriteMode -> unit
Public Sub WriteXml (writer As XmlWriter, mode As XmlWriteMode)

參數

writer
XmlWriter XmlWriter XmlWriter XmlWriter

寫入文件所用的 XmlWriterThe XmlWriter used to write the document.

mode
XmlWriteMode XmlWriteMode XmlWriteMode XmlWriteMode

其中一個 XmlWriteMode 值。One of the XmlWriteMode values.

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that include the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(Stream, XmlWriteMode, Boolean) WriteXml(Stream, XmlWriteMode, Boolean) WriteXml(Stream, XmlWriteMode, Boolean)

使用指定的 DataTable,寫入 XmlWriteMode 的目前資料 (並選擇性寫入結構描述) 至指定的檔案。Writes the current data, and optionally the schema, for the DataTable to the specified file using the specified XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

public:
 void WriteXml(System::IO::Stream ^ stream, System::Data::XmlWriteMode mode, bool writeHierarchy);
public void WriteXml (System.IO.Stream stream, System.Data.XmlWriteMode mode, bool writeHierarchy);
member this.WriteXml : System.IO.Stream * System.Data.XmlWriteMode * bool -> unit

參數

stream
Stream Stream Stream Stream

將資料寫入其中的資料流。The stream to which the data will be written.

mode
XmlWriteMode XmlWriteMode XmlWriteMode XmlWriteMode

其中一個 XmlWriteMode 值。One of the XmlWriteMode values.

writeHierarchy
Boolean Boolean Boolean Boolean

如果為 true,就會寫入目前資料表及它所有子代的內容。If true, write the contents of the current table and all its descendants. 如果為 false (預設值),就只會寫入目前資料表的資料。If false (the default value), write the data for the current table only.

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that includes the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

一般來說, WriteXml方法只會儲存目前資料表的資料。Normally, the WriteXml method saves data only for the current table. 方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that includes the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

一般來說, WriteXml方法只會針對目前資料表寫入資料。Normally the WriteXml method writes the data only for the current table. 若要寫入目前資料表及其所有子系(相關資料表)的資料,請呼叫方法writeHierarchy ,並將參數設定為。 trueTo write the data for the current table and all of its descendant, related tables, call the method with the writeHierarchy parameter set to true.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(TextWriter, XmlWriteMode, Boolean) WriteXml(TextWriter, XmlWriteMode, Boolean) WriteXml(TextWriter, XmlWriteMode, Boolean) WriteXml(TextWriter, XmlWriteMode, Boolean)

使用指定的 DataTableTextWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataTable using the specified TextWriter and XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

public:
 void WriteXml(System::IO::TextWriter ^ writer, System::Data::XmlWriteMode mode, bool writeHierarchy);
public void WriteXml (System.IO.TextWriter writer, System.Data.XmlWriteMode mode, bool writeHierarchy);
member this.WriteXml : System.IO.TextWriter * System.Data.XmlWriteMode * bool -> unit
Public Sub WriteXml (writer As TextWriter, mode As XmlWriteMode, writeHierarchy As Boolean)

參數

writer
TextWriter TextWriter TextWriter TextWriter

寫入文件所用的 TextWriterThe TextWriter used to write the document.

mode
XmlWriteMode XmlWriteMode XmlWriteMode XmlWriteMode

其中一個 XmlWriteMode 值。One of the XmlWriteMode values.

writeHierarchy
Boolean Boolean Boolean Boolean

如果為 true,就會寫入目前資料表及它所有子代的內容。If true, write the contents of the current table and all its descendants. 如果為 false (預設值),就只會寫入目前資料表的資料。If false (the default value), write the data for the current table only.

範例

下列主控台應用程式會建立DataTable兩個實例, DataSet並將每個實例DataRelation新增至、建立與這兩個數據表WriteXml相關的,然後使用方法將包含在父資料表中的資料寫入至TextWriter.The following console application creates two DataTable instances, adds each to a DataSet, creates a DataRelation relating the two tables, and then uses the WriteXml method to write the data contained within the parent table to a TextWriter. 此範例會示範將writeHierarchy參數設定為其每個值時的行為。The example demonstrates the behavior when setting the writeHierarchy parameter to each of its values.

static void Main()
{
    DataSet ds = new DataSet();
    DataTable customerTable = GetCustomers();
    DataTable orderTable = GetOrders();

    ds.Tables.Add(customerTable);
    ds.Tables.Add(orderTable);
    ds.Relations.Add("CustomerOrder",
        new DataColumn[] { customerTable.Columns[0] },
        new DataColumn[] { orderTable.Columns[1] }, true);

    System.IO.StringWriter writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, false);
    PrintOutput(writer, "Customer table, without hierarchy");

    writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, true);
    PrintOutput(writer, "Customer table, with hierarchy");

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static DataTable GetCustomers()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(System.Int32));
    table.Columns.Add("Name", typeof(System.String));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    table.AcceptChanges();
    return table;
}

private static DataTable GetOrders()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create three columns; OrderID, CustomerID, and OrderDate.
    table.Columns.Add(new DataColumn("OrderID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("CustomerID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("OrderDate", typeof(System.DateTime)));

    // Set the OrderID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { table.Columns[0] };

    table.Rows.Add(new object[] { 1, 1, "12/2/2003" });
    table.Rows.Add(new object[] { 2, 1, "1/3/2004" });
    table.Rows.Add(new object[] { 3, 2, "11/13/2004" });
    table.Rows.Add(new object[] { 4, 3, "5/16/2004" });
    table.Rows.Add(new object[] { 5, 3, "5/22/2004" });
    table.Rows.Add(new object[] { 6, 4, "6/15/2004" });
    table.AcceptChanges();
    return table;
}

private static void PrintOutput(System.IO.TextWriter writer, 
    string caption)
{
    Console.WriteLine("==============================");
    Console.WriteLine(caption);
    Console.WriteLine("==============================");
    Console.WriteLine(writer.ToString());
}
Sub Main()
  Dim ds As New DataSet
  Dim customerTable As DataTable = GetCustomers()
  Dim orderTable As DataTable = GetOrders()

  ds.Tables.Add(customerTable)
  ds.Tables.Add(orderTable)
  ds.Relations.Add("CustomerOrder", _
   New DataColumn() {customerTable.Columns(0)}, _
   New DataColumn() {orderTable.Columns(1)}, True)

  Dim writer As New System.IO.StringWriter
  customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, False)
  PrintOutput(writer, "Customer table, without hierarchy")

  writer = New System.IO.StringWriter
  customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, True)
  PrintOutput(writer, "Customer table, with hierarchy")

  Console.WriteLine("Press any key to continue.")
  Console.ReadKey()
End Sub

Private Function GetOrders() As DataTable
  ' Create sample Customers table, in order
  ' to demonstrate the behavior of the DataTableReader.
  Dim table As New DataTable

  ' Create three columns, OrderID, CustomerID, and OrderDate.
  table.Columns.Add(New DataColumn("OrderID", GetType(System.Int32)))
  table.Columns.Add(New DataColumn("CustomerID", GetType(System.Int32)))
  table.Columns.Add(New DataColumn("OrderDate", GetType(System.DateTime)))

  ' Set the OrderID column as the primary key column.
  table.PrimaryKey = New DataColumn() {table.Columns(0)}

  table.Rows.Add(New Object() {1, 1, #12/2/2003#})
  table.Rows.Add(New Object() {2, 1, #1/3/2004#})
  table.Rows.Add(New Object() {3, 2, #11/13/2004#})
  table.Rows.Add(New Object() {4, 3, #5/16/2004#})
  table.Rows.Add(New Object() {5, 3, #5/22/2004#})
  table.Rows.Add(New Object() {6, 4, #6/15/2004#})
  table.AcceptChanges()
  Return table
End Function

Private Function GetCustomers() As DataTable
  ' Create sample Customers table, in order
  ' to demonstrate the behavior of the DataTableReader.
  Dim table As New DataTable

  ' Create two columns, ID and Name.
  Dim idColumn As DataColumn = table.Columns.Add("ID", _
      GetType(System.Int32))
  table.Columns.Add("Name", GetType(System.String))

  ' Set the ID column as the primary key column.
  table.PrimaryKey = New DataColumn() {idColumn}

  table.Rows.Add(New Object() {1, "Mary"})
  table.Rows.Add(New Object() {2, "Andy"})
  table.Rows.Add(New Object() {3, "Peter"})
  table.Rows.Add(New Object() {4, "Russ"})
  table.AcceptChanges()
  Return table
End Function

Private Sub PrintOutput( _
   ByVal writer As System.IO.TextWriter, ByVal caption As String)

  Console.WriteLine("==============================")
  Console.WriteLine(caption)
  Console.WriteLine("==============================")
  Console.WriteLine(writer.ToString())
End Sub

此範例會在主控台視窗中顯示以下輸出:The example displays the following output in the console window:

==============================  
Customer table, without hierarchy  
==============================  
<NewDataSet>  
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema  
" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">  
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table1">  
      <xs:complexType>  
        <xs:choice minOccurs="0" maxOccurs="unbounded">  
          <xs:element name="Table1">  
            <xs:complexType>  
              <xs:sequence>  
                <xs:element name="ID" type="xs:int" />  
                <xs:element name="Name" type="xs:string" minOccurs="0" />  
              </xs:sequence>  
            </xs:complexType>  
          </xs:element>  
        </xs:choice>  
      </xs:complexType>  
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">  
        <xs:selector xpath=".//Table1" />  
        <xs:field xpath="ID" />  
      </xs:unique>  
    </xs:element>  
  </xs:schema>  
  <Table1>  
    <ID>1</ID>  
    <Name>Mary</Name>  
  </Table1>  
  <Table1>  
    <ID>2</ID>  
    <Name>Andy</Name>  
  </Table1>  
  <Table1>  
    <ID>3</ID>  
    <Name>Peter</Name>  
  </Table1>  
  <Table1>  
    <ID>4</ID>  
    <Name>Russ</Name>  
  </Table1>  
</NewDataSet>  
==============================  
Customer table, with hierarchy  
==============================  
<NewDataSet>  
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema  
" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">  
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table1">  
      <xs:complexType>  
        <xs:choice minOccurs="0" maxOccurs="unbounded">  
          <xs:element name="Table1">  
            <xs:complexType>  
              <xs:sequence>  
                <xs:element name="ID" type="xs:int" />  
                <xs:element name="Name" type="xs:string" minOccurs="0" />  
              </xs:sequence>  
            </xs:complexType>  
          </xs:element>  
          <xs:element name="Table2">  
            <xs:complexType>  
              <xs:sequence>  
                <xs:element name="OrderID" type="xs:int" />  
                <xs:element name="CustomerID" type="xs:int" minOccurs="0" />  
                <xs:element name="OrderDate" type="xs:dateTime" minOccurs="0" />  

              </xs:sequence>  
            </xs:complexType>  
          </xs:element>  
        </xs:choice>  
      </xs:complexType>  
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">  
        <xs:selector xpath=".//Table1" />  
        <xs:field xpath="ID" />  
      </xs:unique>  
      <xs:unique name="Table2_Constraint1" msdata:ConstraintName="Constraint1" m  
sdata:PrimaryKey="true">  
        <xs:selector xpath=".//Table2" />  
        <xs:field xpath="OrderID" />  
      </xs:unique>  
      <xs:keyref name="CustomerOrder" refer="Constraint1">  
        <xs:selector xpath=".//Table2" />  
        <xs:field xpath="CustomerID" />  
      </xs:keyref>  
    </xs:element>  
  </xs:schema>  
  <Table1>  
    <ID>1</ID>  
    <Name>Mary</Name>  
  </Table1>  
  <Table1>  
    <ID>2</ID>  
    <Name>Andy</Name>  
  </Table1>  
  <Table1>  
    <ID>3</ID>  
    <Name>Peter</Name>  
  </Table1>  
  <Table1>  
    <ID>4</ID>  
    <Name>Russ</Name>  
  </Table1>  
  <Table2>  
    <OrderID>1</OrderID>  
    <CustomerID>1</CustomerID>  
    <OrderDate>2003-12-02T00:00:00.0000000-08:00</OrderDate>  
  </Table2>  
  <Table2>  
    <OrderID>2</OrderID>  
    <CustomerID>1</CustomerID>  
    <OrderDate>2004-01-03T00:00:00.0000000-08:00</OrderDate>  
  </Table2>  
  <Table2>  
    <OrderID>3</OrderID>  
    <CustomerID>2</CustomerID>  
    <OrderDate>2004-11-13T00:00:00.0000000-08:00</OrderDate>  
  </Table2>  
  <Table2>  
    <OrderID>4</OrderID>  
    <CustomerID>3</CustomerID>  
    <OrderDate>2004-05-16T00:00:00.0000000-07:00</OrderDate>  
  </Table2>  
  <Table2>  
    <OrderID>5</OrderID>  
    <CustomerID>3</CustomerID>  
    <OrderDate>2004-05-22T00:00:00.0000000-07:00</OrderDate>  
  </Table2>  
  <Table2>  
    <OrderID>6</OrderID>  
    <CustomerID>4</CustomerID>  
    <OrderDate>2004-06-15T00:00:00.0000000-07:00</OrderDate>  
  </Table2>  
</NewDataSet>  

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that includes the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

一般來說, WriteXml方法只會儲存目前資料表的資料。Normally, the WriteXml method saves data only for the current table. 方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that includes the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

一般來說, WriteXml方法只會針對目前資料表寫入資料。Normally the WriteXml method writes the data only for the current table. 若要寫入目前資料表及其所有子系(相關資料表)的資料,請呼叫方法writeHierarchy ,並將參數設定為。 trueTo write the data for the current table and all of its descendant, related tables, call the method with the writeHierarchy parameter set to true.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(String, Boolean) WriteXml(String, Boolean) WriteXml(String, Boolean) WriteXml(String, Boolean)

使用指定的檔案,將 DataTable 目前的內容寫成 XML。Writes the current contents of the DataTable as XML using the specified file. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

public:
 void WriteXml(System::String ^ fileName, bool writeHierarchy);
public void WriteXml (string fileName, bool writeHierarchy);
member this.WriteXml : string * bool -> unit
Public Sub WriteXml (fileName As String, writeHierarchy As Boolean)

參數

fileName
String String String String

要寫入 XML 資料的檔案。The file to which to write the XML data.

writeHierarchy
Boolean Boolean Boolean Boolean

如果為 true,就會寫入目前資料表及它所有子代的內容。If true, write the contents of the current table and all its descendants. 如果為 false (預設值),就只會寫入目前資料表的資料。If false (the default value), write the data for the current table only.

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that includes the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

一般來說, WriteXml方法只會針對目前資料表寫入資料。Normally the WriteXml method writes the data only for the current table. 若要寫入目前資料表及其所有子系(相關資料表)的資料,請呼叫方法writeHierarchy ,並將參數設定為。 trueTo write the data for the current table and all of its descendant, related tables, call the method with the writeHierarchy parameter set to true.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(Stream, Boolean) WriteXml(Stream, Boolean) WriteXml(Stream, Boolean)

使用指定的 Stream,將 DataTable 目前的內容寫入為 XML。Writes the current contents of the DataTable as XML using the specified Stream. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

public:
 void WriteXml(System::IO::Stream ^ stream, bool writeHierarchy);
public void WriteXml (System.IO.Stream stream, bool writeHierarchy);
member this.WriteXml : System.IO.Stream * bool -> unit

參數

stream
Stream Stream Stream Stream

將資料寫入其中的資料流。The stream to which the data will be written.

writeHierarchy
Boolean Boolean Boolean Boolean

如果為 true,就會寫入目前資料表及它所有子代的內容。If true, write the contents of the current table and all its descendants. 如果為 false (預設值),就只會寫入目前資料表的資料。If false (the default value), write the data for the current table only.

備註

使用方法,即可將的架構DataTable寫入 XML 檔。 WriteXmlSchemaUse the WriteXmlSchema method to write the schema for a DataTable to an XML document. 架構包括資料表、關聯和條件約束定義。The schema includes table, relation, and constraint definitions.

XML 架構是使用 XSD 標準撰寫的。The XML schema is written using the XSD standard.

若要將資料寫入 XML 檔,請使用WriteXml方法。To write the data to an XML document, use the WriteXml method.

一般來說, WriteXml方法只會針對目前資料表寫入資料。Normally the WriteXml method writes the data only for the current table. 若要寫入目前資料表及其所有子系(相關資料表)的資料,請呼叫方法writeHierarchy ,並將參數設定為。 trueTo write the data for the current table and all of its descendant, related tables, call the method with the writeHierarchy parameter set to true.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(XmlWriter, XmlWriteMode, Boolean) WriteXml(XmlWriter, XmlWriteMode, Boolean) WriteXml(XmlWriter, XmlWriteMode, Boolean) WriteXml(XmlWriter, XmlWriteMode, Boolean)

使用指定的 DataTableXmlWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataTable using the specified XmlWriter and XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

public:
 void WriteXml(System::Xml::XmlWriter ^ writer, System::Data::XmlWriteMode mode, bool writeHierarchy);
public void WriteXml (System.Xml.XmlWriter writer, System.Data.XmlWriteMode mode, bool writeHierarchy);
member this.WriteXml : System.Xml.XmlWriter * System.Data.XmlWriteMode * bool -> unit
Public Sub WriteXml (writer As XmlWriter, mode As XmlWriteMode, writeHierarchy As Boolean)

參數

writer
XmlWriter XmlWriter XmlWriter XmlWriter

寫入文件所用的 XmlWriterThe XmlWriter used to write the document.

mode
XmlWriteMode XmlWriteMode XmlWriteMode XmlWriteMode

其中一個 XmlWriteMode 值。One of the XmlWriteMode values.

writeHierarchy
Boolean Boolean Boolean Boolean

如果為 true,就會寫入目前資料表及它所有子代的內容。If true, write the contents of the current table and all its descendants. 如果為 false (預設值),就只會寫入目前資料表的資料。If false (the default value), write the data for the current table only.

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that include the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

一般來說, WriteXml方法只會針對目前資料表寫入資料。Normally the WriteXml method writes the data only for the current table. 若要寫入目前資料表及其整個子代(相關資料表)的資料,請呼叫方法writeHierarchy ,並將參數設定為。 trueTo write the data for the current table and its entire descendant, related tables, call the method with the writeHierarchy parameter set to true.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(String) WriteXml(String) WriteXml(String) WriteXml(String)

使用指定的檔案,將 DataTable 目前的內容寫成 XML。Writes the current contents of the DataTable as XML using the specified file.

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

參數

fileName
String String String String

要寫入 XML 資料的檔案。The file to which to write the XML data.

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that includes the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(TextWriter) WriteXml(TextWriter) WriteXml(TextWriter) WriteXml(TextWriter)

使用指定的 TextWriter,將 DataTable 目前的內容寫入為 XML。Writes the current contents of the DataTable as XML using the specified TextWriter.

public:
 void WriteXml(System::IO::TextWriter ^ writer);
public void WriteXml (System.IO.TextWriter writer);
member this.WriteXml : System.IO.TextWriter -> unit
Public Sub WriteXml (writer As TextWriter)

參數

writer
TextWriter TextWriter TextWriter TextWriter

用來撰寫內容的 TextWriterThe TextWriter with which to write the content.

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that includes the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(Stream) WriteXml(Stream) WriteXml(Stream)

使用指定的 Stream,將 DataTable 目前的內容寫入為 XML。Writes the current contents of the DataTable as XML using the specified Stream.

public:
 void WriteXml(System::IO::Stream ^ stream);
public void WriteXml (System.IO.Stream stream);
member this.WriteXml : System.IO.Stream -> unit

參數

stream
Stream Stream Stream Stream

將資料寫入其中的資料流。The stream to which the data will be written.

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that includes the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(String, XmlWriteMode, Boolean) WriteXml(String, XmlWriteMode, Boolean) WriteXml(String, XmlWriteMode, Boolean) WriteXml(String, XmlWriteMode, Boolean)

使用指定的檔案和 DataTable,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataTable using the specified file and XmlWriteMode. 若要寫入結構描述,請設定 mode 參數的值為 WriteSchemaTo write the schema, set the value for the mode parameter to WriteSchema. 若要儲存資料表及其所有子系的資料,請將 writeHierarchy 參數設定為 trueTo save the data for the table and all its descendants, set the writeHierarchy parameter to true.

public:
 void WriteXml(System::String ^ fileName, System::Data::XmlWriteMode mode, bool writeHierarchy);
public void WriteXml (string fileName, System.Data.XmlWriteMode mode, bool writeHierarchy);
member this.WriteXml : string * System.Data.XmlWriteMode * bool -> unit
Public Sub WriteXml (fileName As String, mode As XmlWriteMode, writeHierarchy As Boolean)

參數

fileName
String String String String

資料將寫入其中的檔案名稱。The name of the file to which the data will be written.

mode
XmlWriteMode XmlWriteMode XmlWriteMode XmlWriteMode

其中一個 XmlWriteMode 值。One of the XmlWriteMode values.

writeHierarchy
Boolean Boolean Boolean Boolean

如果為 true,就會寫入目前資料表及它所有子代的內容。If true, write the contents of the current table and all its descendants. 如果為 false (預設值),就只會寫入目前資料表的資料。If false (the default value), write the data for the current table only.

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that include the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

一般來說, WriteXml方法只會儲存目前資料表的資料。Normally, the WriteXml method saves data only for the current table. 如果您想要儲存目前資料表和所有架構的資料, WriteXml方法會提供一種方式,將資料或DataTable中的資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。If you want to save the data for the current table and all of schema, the WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that include the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

一般來說, WriteXml方法只會針對目前資料表寫入資料。Normally the WriteXml method writes the data only for the current table. 若要寫入目前資料表和所有子系(相關資料表)的資料,請呼叫方法writeHierarchy ,並將參數設定為。 trueTo write the data for the current table and all descendant, related tables, call the method with the writeHierarchy parameter set to true.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

WriteXml(XmlWriter) WriteXml(XmlWriter) WriteXml(XmlWriter) WriteXml(XmlWriter)

使用指定的 XmlWriter,將 DataTable 目前的內容寫入為 XML。Writes the current contents of the DataTable as XML using the specified XmlWriter.

public:
 void WriteXml(System::Xml::XmlWriter ^ writer);
public void WriteXml (System.Xml.XmlWriter writer);
member this.WriteXml : System.Xml.XmlWriter -> unit
Public Sub WriteXml (writer As XmlWriter)

參數

writer
XmlWriter XmlWriter XmlWriter XmlWriter

要用來寫入內容的 XmlWriterThe XmlWriter with which to write the contents.

實作

備註

方法提供一種方式,只會將資料或中的DataTable資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請使用其中一個包含XmlWriteMode參數的多載,並將其值設定為。 WriteSchemaTo write both data and schema, use one of the overloads that include the XmlWriteMode parameter, and set its value to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataTable,請ReadXml使用方法。To read XML data, or both schema and data into the DataTable, use the ReadXml method. 若只要讀取架構,請使用ReadXmlSchema方法。To read just the schema, use the ReadXmlSchema method.

注意

如果要讀取或寫入IDynamicMetaObjectProvider IXmlSerializable中的資料行類型來執行和未實作為,則會擲回。InvalidOperationException DataRowAn InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

另請參閱

適用於