DataTable.WriteXml 方法

定義

將目前的內容 DataTable 以 XML 格式寫入。

多載

名稱 Description
WriteXml(TextWriter, Boolean)

將目前的 內容 DataTable 以指定的 TextWriterXML 格式寫入。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

WriteXml(TextWriter, XmlWriteMode)

會寫入目前的資料,並可選擇性地寫入結構, DataTable 使用指定的 TextWriterXmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema

WriteXml(String, XmlWriteMode)

利用指定的檔案和XmlWriteMode寫入目前的資料,並可選擇性地寫入結構DataTable。 要撰寫結構,將參數值設 modeWriteSchema

WriteXml(Stream, XmlWriteMode)

將目前的資料,以及可選的結構 DataTable ,寫入指定檔案,使用指定的 XmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema

WriteXml(XmlWriter, Boolean)

將目前的 內容 DataTable 以指定的 XmlWriterXML 格式寫入。

WriteXml(XmlWriter, XmlWriteMode)

會寫入目前的資料,並可選擇性地寫入結構, DataTable 使用指定的 XmlWriterXmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema

WriteXml(Stream, XmlWriteMode, Boolean)

將目前的資料,以及可選的結構 DataTable ,寫入指定檔案,使用指定的 XmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

WriteXml(TextWriter, XmlWriteMode, Boolean)

會寫入目前的資料,並可選擇性地寫入結構, DataTable 使用指定的 TextWriterXmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

WriteXml(String, Boolean)

使用指定檔案將目前的內容 DataTable 寫入為 XML。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

WriteXml(Stream, Boolean)

將目前的 內容 DataTable 以指定的 StreamXML 格式寫入。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

WriteXml(XmlWriter, XmlWriteMode, Boolean)

會寫入目前的資料,並可選擇性地寫入結構, DataTable 使用指定的 XmlWriterXmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

WriteXml(String)

使用指定檔案將目前的內容 DataTable 寫入為 XML。

WriteXml(TextWriter)

將目前的 內容 DataTable 以指定的 TextWriterXML 格式寫入。

WriteXml(Stream)

將目前的 內容 DataTable 以指定的 StreamXML 格式寫入。

WriteXml(String, XmlWriteMode, Boolean)

利用指定的檔案和XmlWriteMode寫入目前的資料,並可選擇性地寫入結構DataTable。 要撰寫結構,將參數值設 modeWriteSchema。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

WriteXml(XmlWriter)

將目前的 內容 DataTable 以指定的 XmlWriterXML 格式寫入。

範例

以下主控台應用程式會建立兩個DataTable實例,分別加入 ,DataSetDataRelation建立一個關聯這兩個資料表的 ,然後使用該WriteXml方法將父資料表中的資料寫入 TextWriter。 範例展示了將參數設定 writeHierarchy 為每個值時的行為。

Note

這個範例展示了如何使用其中一個超載版本的 WriteXml。 其他可能可用的範例,請參閱個別超載主題。

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

這個範例會在主控台視窗中顯示下列輸出:

==============================
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>

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載之一,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

WriteXml(TextWriter, Boolean)

將目前的 內容 DataTable 以指定的 TextWriterXML 格式寫入。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

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

writeHierarchy
Boolean

true,則寫入目前資料表及其所有後代的資料。 如果 false (預設值),則只寫入目前資料表的資料。

範例

以下主控台應用程式會建立兩個DataTable實例,分別加入 ,DataSetDataRelation建立一個關聯這兩個資料表的 ,然後使用該WriteXml方法將父資料表中的資料寫入 TextWriter。 範例展示了將參數設 writeHierarchytrue時的行為。

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

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載,並將其值設為 WriteSchema

通常該 WriteXml 方法只寫入目前資料表的資料。 要寫入目前資料表及其所有後代相關資料表的資料,請呼叫將 writeHierarchy 參數設為 true的方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(TextWriter, XmlWriteMode)

會寫入目前的資料,並可選擇性地寫入結構, DataTable 使用指定的 TextWriterXmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema

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 用來撰寫文件。

mode
XmlWriteMode

這是其中一項 XmlWriteMode 價值。

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(String, XmlWriteMode)

利用指定的檔案和XmlWriteMode寫入目前的資料,並可選擇性地寫入結構DataTable。 要撰寫結構,將參數值設 modeWriteSchema

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

資料將被寫入的檔案名稱。

mode
XmlWriteMode

這是其中一項 XmlWriteMode 價值。

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載之一,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(Stream, XmlWriteMode)

將目前的資料,以及可選的結構 DataTable ,寫入指定檔案,使用指定的 XmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema

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
Public Sub WriteXml (stream As Stream, mode As XmlWriteMode)

參數

stream
Stream

資料將被寫入的串流。

mode
XmlWriteMode

這是其中一項 XmlWriteMode 價值。

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(XmlWriter, Boolean)

將目前的 內容 DataTable 以指定的 XmlWriterXML 格式寫入。

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用來撰寫內容。

writeHierarchy
Boolean

true,則寫入目前資料表及其所有後代的資料。 如果 false (預設值),則只寫入目前資料表的資料。

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載之一,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

通常該 WriteXml 方法只寫入目前資料表的資料。 要寫入目前資料表及其整個後代相關資料表的資料,請呼叫將 writeHierarchy 參數設為 true的方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(XmlWriter, XmlWriteMode)

會寫入目前的資料,並可選擇性地寫入結構, DataTable 使用指定的 XmlWriterXmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema

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 用來撰寫文件。

mode
XmlWriteMode

這是其中一項 XmlWriteMode 價值。

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載之一,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(Stream, XmlWriteMode, Boolean)

將目前的資料,以及可選的結構 DataTable ,寫入指定檔案,使用指定的 XmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

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
Public Sub WriteXml (stream As Stream, mode As XmlWriteMode, writeHierarchy As Boolean)

參數

stream
Stream

資料將被寫入的串流。

mode
XmlWriteMode

這是其中一項 XmlWriteMode 價值。

writeHierarchy
Boolean

true,則寫入目前資料表及其所有後代的資料。 如果 false (預設值),則只寫入目前資料表的資料。

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

通常,此 WriteXml 方法只會儲存目前資料表的資料。 此 WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

通常該 WriteXml 方法只寫入目前資料表的資料。 要寫入目前資料表及其所有後代相關資料表的資料,請呼叫將 writeHierarchy 參數設為 true的方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(TextWriter, XmlWriteMode, Boolean)

會寫入目前的資料,並可選擇性地寫入結構, DataTable 使用指定的 TextWriterXmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

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 用來撰寫文件。

mode
XmlWriteMode

這是其中一項 XmlWriteMode 價值。

writeHierarchy
Boolean

true,則寫入目前資料表及其所有後代的資料。 如果 false (預設值),則只寫入目前資料表的資料。

範例

以下主控台應用程式會建立兩個DataTable實例,分別加入 ,DataSetDataRelation建立一個關聯這兩個資料表的 ,然後使用該WriteXml方法將父資料表中的資料寫入 TextWriter。 範例展示了將參數設定 writeHierarchy 為每個值時的行為。

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

這個範例會在主控台視窗中顯示下列輸出:

==============================
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>

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

通常,此 WriteXml 方法只會儲存目前資料表的資料。 此 WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

通常該 WriteXml 方法只寫入目前資料表的資料。 要寫入目前資料表及其所有後代相關資料表的資料,請呼叫將 writeHierarchy 參數設為 true的方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(String, Boolean)

使用指定檔案將目前的內容 DataTable 寫入為 XML。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

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

用來寫入 XML 資料的檔案。

writeHierarchy
Boolean

true,則寫入目前資料表及其所有後代的資料。 如果 false (預設值),則只寫入目前資料表的資料。

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

通常該 WriteXml 方法只寫入目前資料表的資料。 要寫入目前資料表及其所有後代相關資料表的資料,請呼叫將 writeHierarchy 參數設為 true的方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(Stream, Boolean)

將目前的 內容 DataTable 以指定的 StreamXML 格式寫入。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

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
Public Sub WriteXml (stream As Stream, writeHierarchy As Boolean)

參數

stream
Stream

資料將被寫入的串流。

writeHierarchy
Boolean

true,則寫入目前資料表及其所有後代的資料。 如果 false (預設值),則只寫入目前資料表的資料。

備註

使用 WriteXmlSchema 將 a 的結構 DataTable 寫成 XML 文件的方法。 該結構包含表格、關聯與約束定義。

XML 架構是依據 XSD 標準撰寫的。

若要將資料寫入 XML 文件,請使用以下 WriteXml 方法。

通常該 WriteXml 方法只寫入目前資料表的資料。 要寫入目前資料表及其所有後代相關資料表的資料,請呼叫將 writeHierarchy 參數設為 true的方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(XmlWriter, XmlWriteMode, Boolean)

會寫入目前的資料,並可選擇性地寫入結構, DataTable 使用指定的 XmlWriterXmlWriteMode。 要撰寫結構,將參數值設 modeWriteSchema。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

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 用來撰寫文件。

mode
XmlWriteMode

這是其中一項 XmlWriteMode 價值。

writeHierarchy
Boolean

true,則寫入目前資料表及其所有後代的資料。 如果 false (預設值),則只寫入目前資料表的資料。

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載之一,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

通常該 WriteXml 方法只寫入目前資料表的資料。 要寫入目前資料表及其整個後代相關資料表的資料,請呼叫將 writeHierarchy 參數設為 true的方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(String)

使用指定檔案將目前的內容 DataTable 寫入為 XML。

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

參數

fileName
String

用來寫入 XML 資料的檔案。

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(TextWriter)

將目前的 內容 DataTable 以指定的 TextWriterXML 格式寫入。

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

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(Stream)

將目前的 內容 DataTable 以指定的 StreamXML 格式寫入。

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

參數

stream
Stream

資料將被寫入的串流。

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(String, XmlWriteMode, Boolean)

利用指定的檔案和XmlWriteMode寫入目前的資料,並可選擇性地寫入結構DataTable。 要撰寫結構,將參數值設 modeWriteSchema。 要儲存資料表及其所有後代的資料,請將參數設 writeHierarchytrue

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

資料將被寫入的檔案名稱。

mode
XmlWriteMode

這是其中一項 XmlWriteMode 價值。

writeHierarchy
Boolean

true,則寫入目前資料表及其所有後代的資料。 如果 false (預設值),則只寫入目前資料表的資料。

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載之一,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

通常,此 WriteXml 方法只會儲存目前資料表的資料。 如果你想儲存目前資料表的資料和整個結構,這個 WriteXml 方法提供一種只寫入資料或同時寫入 A DataTable 的資料和結構到一個 XML 文件的方法,而這個 WriteXmlSchema 方法則只寫入結構本身。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載之一,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

通常該 WriteXml 方法只寫入目前資料表的資料。 要寫入目前資料表及所有相關後代資料表的資料,請呼叫參數 writeHierarchy 設為 true的方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於

WriteXml(XmlWriter)

將目前的 內容 DataTable 以指定的 XmlWriterXML 格式寫入。

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用來撰寫內容。

備註

WriteXml 方法提供一種僅將資料或同時從 中 DataTable 寫入資料與結構到XML文件的方法,而該 WriteXmlSchema 方法則僅寫入結構。 要同時寫入資料與結構,請使用包含參數的 XmlWriteMode 超載之一,並將其值設為 WriteSchema

請注意,與ReadXmlSchema方法分別也是ReadXml同理。 若要將 XML 資料,或同時將結構與資料讀取到 DataTable,請使用該 ReadXml 方法。 如果只讀結構,請使用該 ReadXmlSchema 方法。

Note

InvalidOperationException若被讀取或寫IDynamicMetaObjectProvider入的欄位DataRow類型將被拋出,且未實作 IXmlSerializable

另請參閱

適用於