DataSet.WriteXml 方法

定義

DataSet 寫入 XML 資料,並選擇性寫入結構描述。

多載

WriteXml(XmlWriter, XmlWriteMode)

使用指定的 DataSetXmlWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。 若要寫入結構描述,請設定 mode 參數的值為 WriteSchema

WriteXml(String, XmlWriteMode)

使用指定的 DataSet,寫入 XmlWriteMode 的目前資料 (並選擇性寫入結構描述) 至指定的檔案。 若要寫入結構描述,請設定 mode 參數的值為 WriteSchema

WriteXml(TextWriter, XmlWriteMode)

使用指定的 DataSetTextWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。 若要寫入結構描述,請設定 mode 參數的值為 WriteSchema

WriteXml(Stream, XmlWriteMode)

使用指定的 DataSetStream,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。 若要寫入結構描述,請設定 mode 參數的值為 WriteSchema

WriteXml(String)

DataSet 的目前資料寫入指定的檔案。

WriteXml(TextWriter)

使用指定的 DataSet,寫入 TextWriter 的目前資料。

WriteXml(XmlWriter)

DataSet 的目前資料寫入指定的 XmlWriter

WriteXml(Stream)

使用指定的 DataSet,寫入 Stream 的目前資料。

WriteXml(XmlWriter, XmlWriteMode)

來源:
DataSet.cs
來源:
DataSet.cs
來源:
DataSet.cs

使用指定的 DataSetXmlWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。 若要寫入結構描述,請設定 mode 參數的值為 WriteSchema

public void WriteXml (System.Xml.XmlWriter? writer, System.Data.XmlWriteMode mode);
public void WriteXml (System.Xml.XmlWriter writer, System.Data.XmlWriteMode mode);

參數

writer
XmlWriter

要用來寫入的 XmlWriter

mode
XmlWriteMode

其中一個 XmlWriteMode 值。

範例

下列範例會 System.IO.FileStream 建立 用來建立新 XmlTextWriter的物件。 物件 XmlTextWriter 會與 方法搭配 WriteXml 使用,以撰寫 XML 檔。

private void WriteXmlToFile(DataSet thisDataSet)
{
    if (thisDataSet == null) { return; }

    // Create a file name to write to.
    string filename = "XmlDoc.xml";

    // Create the FileStream to write with.
    System.IO.FileStream stream = new System.IO.FileStream
        (filename, System.IO.FileMode.Create);

    // Create an XmlTextWriter with the fileStream.
    System.Xml.XmlTextWriter xmlWriter =
        new System.Xml.XmlTextWriter(stream,
        System.Text.Encoding.Unicode);

    // Write to the file with the WriteXml method.
    thisDataSet.WriteXml(xmlWriter);
    xmlWriter.Close();
}

備註

方法 WriteXml 提供一種方式,只寫入數據,或將數據和架構從 DataSet 寫入 XML 檔,而 WriteXmlSchema 方法只會寫入架構。 若要同時寫入資料和架構,請將 mode 參數設定為 WriteSchema

請注意, ReadXmlReadXmlSchema 方法也是如此。 若要讀取 XML 數據,或同時將架構和數據讀入 DataSet,請使用 ReadXml 方法。 若要唯讀取架構,請使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果要讀取或寫入至 實IDynamicMetaObjectProvider作且未實IXmlSerializable作 的資料行類型,DataRow則會擲回 。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(String, XmlWriteMode)

來源:
DataSet.cs
來源:
DataSet.cs
來源:
DataSet.cs

使用指定的 DataSet,寫入 XmlWriteMode 的目前資料 (並選擇性寫入結構描述) 至指定的檔案。 若要寫入結構描述,請設定 mode 參數的值為 WriteSchema

public void WriteXml (string fileName, System.Data.XmlWriteMode mode);

參數

fileName
String

要寫入的目的檔案名稱 (包括路徑)。

mode
XmlWriteMode

其中一個 XmlWriteMode 值。

例外狀況

FileIOPermission 並不是設定為 Write

範例

下列範例會 WriteXml 使用 方法來撰寫 XML 檔。

private void WriteXmlToFile(DataSet thisDataSet)
{
    if (thisDataSet == null) { return; }

    // Create a file name to write to.
    string filename = "XmlDoc.xml";

    // Write to the file with the WriteXml method.
    thisDataSet.WriteXml(filename);
}

備註

方法 WriteXml 提供一種方式,只寫入數據,或將數據和架構從 DataSet 寫入 XML 檔,而 WriteXmlSchema 方法只會寫入架構。 若要同時寫入資料和架構,請將 mode 參數設定為 WriteSchema

請注意, ReadXmlReadXmlSchema 方法也是如此。 若要讀取 XML 數據,或同時將架構和數據讀入 DataSet,請使用 ReadXml 方法。 若要唯讀取架構,請使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果要讀取或寫入至 實IDynamicMetaObjectProvider作且未實IXmlSerializable作 的資料行類型,DataRow則會擲回 。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(TextWriter, XmlWriteMode)

來源:
DataSet.cs
來源:
DataSet.cs
來源:
DataSet.cs

使用指定的 DataSetTextWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。 若要寫入結構描述,請設定 mode 參數的值為 WriteSchema

public void WriteXml (System.IO.TextWriter? writer, System.Data.XmlWriteMode mode);
public void WriteXml (System.IO.TextWriter writer, System.Data.XmlWriteMode mode);

參數

writer
TextWriter

用來寫入文件的 TextWriter 物件。

mode
XmlWriteMode

其中一個 XmlWriteMode 值。

範例

下列範例會先建立具有一個DataTable、兩個資料行和十個資料列的簡單 DataSet 。 叫用 方法會將 DataSet 架構和數據寫入磁碟 WriteXml 。 系統會建立第二 DataSet 個 ,並使用 ReadXml 方法來填入架構和數據。

private void DemonstrateReadWriteXMLDocumentWithFileStream()
{
    // Create a DataSet with one table and two columns.
    DataSet originalDataSet = new DataSet("dataSet");
    DataTable table = new DataTable("table");
    DataColumn idColumn = new DataColumn("id",
        Type.GetType("System.Int32"));
    idColumn.AutoIncrement= true;

    DataColumn itemColumn = new DataColumn("item");
    table.Columns.Add(idColumn);
    table.Columns.Add(itemColumn);
    originalDataSet.Tables.Add(table);
    // Add ten rows.

    DataRow newRow;
    for(int i = 0; i < 10; i++)
    {
        newRow = table.NewRow();
        newRow["item"]= "item " + i;
        table.Rows.Add(newRow);
    }
    originalDataSet.AcceptChanges();

    // Print out values of each table in the DataSet
    // using the function defined below.
    PrintValues(originalDataSet, "Original DataSet");

    // Write the schema and data to XML file with FileStream.
    string xmlFilename = "XmlDocument.xml";
    System.IO.FileStream streamWrite = new System.IO.FileStream
        (xmlFilename, System.IO.FileMode.Create);

    // Use WriteXml to write the XML document.
    originalDataSet.WriteXml(streamWrite);

    // Close the FileStream.
    streamWrite.Close();

    // Dispose of the original DataSet.
    originalDataSet.Dispose();
    // Create a new DataSet.
    DataSet newDataSet = new DataSet("New DataSet");

    // Read the XML document back in.
    // Create new FileStream to read schema with.
    System.IO.FileStream streamRead = new System.IO.FileStream
        (xmlFilename,System.IO.FileMode.Open);
    newDataSet.ReadXml(streamRead);

    // Print out values of each table in the DataSet
    // using the function defined below.
    PrintValues(newDataSet,"New DataSet");
}

private void PrintValues(DataSet dataSet, string label)
{
    Console.WriteLine("\n" + label);
    foreach(DataTable table in dataSet.Tables)
    {
        Console.WriteLine("TableName: " + table.TableName);
        foreach(DataRow row in table.Rows)
        {
            foreach(DataColumn column in table.Columns)
            {
                Console.Write("\table " + row[column] );
            }
            Console.WriteLine();
        }
    }
}

備註

方法 WriteXml 提供一種方式,只寫入數據,或將數據和架構從 DataSet 寫入 XML 檔,而 WriteXmlSchema 方法只會寫入架構。 若要同時寫入資料和架構,請將 mode 參數設定為 WriteSchema

請注意, ReadXmlReadXmlSchema 方法也是如此。 若要讀取 XML 數據,或同時將架構和數據讀入 DataSet,請使用 ReadXml 方法。 若要唯讀取架構,請使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果要讀取或寫入至 實IDynamicMetaObjectProvider作且未實IXmlSerializable作 的資料行類型,DataRow則會擲回 。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(Stream, XmlWriteMode)

來源:
DataSet.cs
來源:
DataSet.cs
來源:
DataSet.cs

使用指定的 DataSetStream,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。 若要寫入結構描述,請設定 mode 參數的值為 WriteSchema

public void WriteXml (System.IO.Stream? stream, System.Data.XmlWriteMode mode);
public void WriteXml (System.IO.Stream stream, System.Data.XmlWriteMode mode);

參數

stream
Stream

用來寫入檔案的 Stream 物件。

mode
XmlWriteMode

其中一個 XmlWriteMode 值。

備註

方法 WriteXml 提供一種方式,只寫入數據,或將數據和架構從 DataSet 寫入 XML 檔,而 WriteXmlSchema 方法只會寫入架構。 若要同時寫入資料和架構,請將 mode 參數設定為 WriteSchema

請注意, ReadXmlReadXmlSchema 方法也是如此。 若要讀取 XML 數據,或同時將架構和數據讀入 DataSet,請使用 ReadXml 方法。 若要唯讀取架構,請使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果要讀取或寫入至 實IDynamicMetaObjectProvider作且未實IXmlSerializable作 的資料行類型,DataRow則會擲回 。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(String)

來源:
DataSet.cs
來源:
DataSet.cs
來源:
DataSet.cs

DataSet 的目前資料寫入指定的檔案。

public void WriteXml (string fileName);

參數

fileName
String

要寫入的目的檔案名稱 (包括路徑)。

例外狀況

FileIOPermission 並不是設定為 Write

備註

方法 WriteXml 提供一種方式,只寫入數據,或將數據和架構從 DataSet 寫入 XML 檔,而 WriteXmlSchema 方法只會寫入架構。 若要同時寫入資料和架構,請使用包含 mode 參數的其中一個多載,並將其值設定為 WriteSchema

請注意, ReadXmlReadXmlSchema 方法也是如此。 若要讀取 XML 數據,或同時將架構和數據讀入 DataSet,請使用 ReadXml 方法。 若要唯讀取架構,請使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果要讀取或寫入至 實IDynamicMetaObjectProvider作且未實IXmlSerializable作 的資料行類型,DataRow則會擲回 。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(TextWriter)

來源:
DataSet.cs
來源:
DataSet.cs
來源:
DataSet.cs

使用指定的 DataSet,寫入 TextWriter 的目前資料。

public void WriteXml (System.IO.TextWriter? writer);
public void WriteXml (System.IO.TextWriter writer);

參數

writer
TextWriter

用來寫入的 TextWriter 物件。

備註

方法 WriteXml 提供一種方式,只寫入數據,或將數據和架構從 DataSet 寫入 XML 檔,而 WriteXmlSchema 方法只會寫入架構。 若要同時寫入資料和架構,請使用包含 mode 參數的其中一個多載,並將其值設定為 WriteSchema

請注意, ReadXmlReadXmlSchema 方法也是如此。 若要讀取 XML 數據,或同時將架構和數據讀入 DataSet,請使用 ReadXml 方法。 若要唯讀取架構,請使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果要讀取或寫入至 實IDynamicMetaObjectProvider作且未實IXmlSerializable作 的資料行類型,DataRow則會擲回 。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(XmlWriter)

來源:
DataSet.cs
來源:
DataSet.cs
來源:
DataSet.cs

DataSet 的目前資料寫入指定的 XmlWriter

public void WriteXml (System.Xml.XmlWriter? writer);
public void WriteXml (System.Xml.XmlWriter writer);

參數

writer
XmlWriter

要用來寫入的 XmlWriter

備註

方法 WriteXml 提供一種方式,只寫入數據,或將數據和架構從 DataSet 寫入 XML 檔,而 WriteXmlSchema 方法只會寫入架構。 若要同時寫入資料和架構,請使用包含 mode 參數的其中一個多載,並將其值設定為 WriteSchema

請注意, ReadXmlReadXmlSchema 方法也是如此。 若要讀取 XML 數據,或同時將架構和數據讀入 DataSet,請使用 ReadXml 方法。 若要唯讀取架構,請使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果要讀取或寫入至 實IDynamicMetaObjectProvider作且未實IXmlSerializable作 的資料行類型,DataRow則會擲回 。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

WriteXml(Stream)

來源:
DataSet.cs
來源:
DataSet.cs
來源:
DataSet.cs

使用指定的 DataSet,寫入 Stream 的目前資料。

public void WriteXml (System.IO.Stream? stream);
public void WriteXml (System.IO.Stream stream);

參數

stream
Stream

用來寫入檔案的 Stream 物件。

範例

下列範例會 System.IO.FileStream 建立物件。 然後,物件會與 方法搭配 WriteXml 使用,以撰寫 XML 檔。

private void WriteXmlToFile(DataSet thisDataSet)
{
    if (thisDataSet == null) { return; }

    // Create a file name to write to.
    string filename = "XmlDoc.xml";

    // Create the FileStream to write with.
    System.IO.FileStream stream = new System.IO.FileStream
        (filename, System.IO.FileMode.Create);

    // Write to the file with the WriteXml method.
    thisDataSet.WriteXml(stream);
}

備註

方法 WriteXml 提供一種方式,只寫入數據,或將數據和架構從 DataSet 寫入 XML 檔,而 WriteXmlSchema 方法只會寫入架構。 若要同時寫入資料和架構,請使用包含 mode 參數的其中一個多載,並將其值設定為 WriteSchema

請注意, ReadXmlReadXmlSchema 方法也是如此。 若要讀取 XML 數據,或同時將架構和數據讀入 DataSet,請使用 ReadXml 方法。 若要唯讀取架構,請使用 ReadXmlSchema 方法。

注意

InvalidOperationException如果要讀取或寫入至 實IDynamicMetaObjectProvider作且未實IXmlSerializable作 的資料行類型,DataRow則會擲回 。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1