DataSet.WriteXml 方法

定義

DataSet 寫入 XML 資料,並選擇性寫入結構描述。Writes XML data, and optionally the schema, from the DataSet.

多載

WriteXml(XmlWriter, XmlWriteMode)

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

WriteXml(String, XmlWriteMode)

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

WriteXml(TextWriter, XmlWriteMode)

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

WriteXml(Stream, XmlWriteMode)

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

WriteXml(String)

DataSet 的目前資料寫入指定的檔案。Writes the current data for the DataSet to the specified file.

WriteXml(TextWriter)

使用指定的 DataSet,寫入 TextWriter 的目前資料。Writes the current data for the DataSet using the specified TextWriter.

WriteXml(XmlWriter)

DataSet 的目前資料寫入指定的 XmlWriterWrites the current data for the DataSet to the specified XmlWriter.

WriteXml(Stream)

使用指定的 DataSet,寫入 Stream 的目前資料。Writes the current data for the DataSet using the specified Stream.

WriteXml(XmlWriter, XmlWriteMode)

使用指定的 DataSetXmlWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataSet 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

要用來寫入的 XmlWriterThe XmlWriter with which to write.

mode
XmlWriteMode

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

範例

下列範例會建立System.IO.FileStream用來建立新XmlTextWriter的物件。The following example creates a System.IO.FileStream object that is used to create a new XmlTextWriter. XmlTextWriter物件會WriteXml與方法搭配使用,以寫入 XML 檔。The XmlTextWriter object is used with the WriteXml method to write an XML document.

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();
}
Private Sub WriteXmlToFile(thisDataSet As DataSet)
    If thisDataSet Is Nothing Then
        Return
    End If

    ' Create a file name to write to.
    Dim filename As String = "XmlDoc.xml"

    ' Create the FileStream to write with.
    Dim stream As New System.IO.FileStream _
       (filename, System.IO.FileMode.Create)

    ' Create an XmlTextWriter with the fileStream.
    Dim xmlWriter As New System.Xml.XmlTextWriter _
       (stream, System.Text.Encoding.Unicode)

    ' Write to the file with the WriteXml method.
    thisDataSet.WriteXml(xmlWriter)
    xmlWriter.Close()
End Sub

備註

方法提供一種方式,只會將資料或中的DataSet資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataSet into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請將mode參數設定WriteSchema為。To write both data and schema, set the mode parameter to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataSet,請ReadXml使用方法。To read XML data, or both schema and data into the DataSet, 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)

使用指定的 DataSet,寫入 XmlWriteMode 的目前資料 (並選擇性寫入結構描述) 至指定的檔案。Writes the current data, and optionally the schema, for the DataSet 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::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

要寫入的目的檔案名稱 (包括路徑)。The file name (including the path) to which to write.

mode
XmlWriteMode

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

例外狀況

FileIOPermission 並不是設定為 WriteFileIOPermission is not set to Write.

範例

下列範例會使用WriteXml方法來撰寫 XML 檔。The following example uses the WriteXml method to write an XML document.

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);
}
Private Sub WriteXmlToFile(thisDataSet As DataSet)
    If thisDataSet Is Nothing Then
        Return
    End If

    ' Create a file name to write to.
    Dim filename As String = "XmlDoc.xml"

    ' Write to the file with the WriteXml method.
    thisDataSet.WriteXml(filename)
End Sub

備註

方法提供一種方式,只會將資料或中的DataSet資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataSet into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請將mode參數設定WriteSchema為。To write both data and schema, set the mode parameter to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataSet,請ReadXml使用方法。To read XML data, or both schema and data into the DataSet, 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.

安全性

FileIOPermission
用於讀取和寫入檔案。for reading and writing files. 相關聯的列舉:FileIOPermissionAccessAssociated enumeration: FileIOPermissionAccess

另請參閱

WriteXml(TextWriter, XmlWriteMode)

使用指定的 DataSetTextWriter,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataSet 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 物件。A TextWriter object used to write the document.

mode
XmlWriteMode

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

範例

下列範例會先建立一個簡單DataSet的, DataTable其中包含一個、兩個數據行和十個數據列。The following example first creates a simple DataSet with one DataTable, two columns, and ten rows. DataSetWriteXml叫用方法,將架構和資料寫入磁片。The DataSet schema and data are written to disk by invoking the WriteXml method. 系統會DataSet建立第二個ReadXml ,並使用方法來填入架構和資料。A second DataSet is created and the ReadXml method is used to fill it with schema and data.

   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();
           }
       }
   }
Private Sub DemonstrateReadWriteXMLDocumentWithFileStream()
    ' Create a DataSet with one table and two columns.
    Dim originalDataSet As New DataSet("dataSet")
    Dim table As New DataTable("table")
    Dim idColumn As New DataColumn("id", _
       Type.GetType("System.Int32"))
    idColumn.AutoIncrement = True

    Dim itemColumn As New DataColumn("item")
    table.Columns.Add(idColumn)
    table.Columns.Add(itemColumn)
    originalDataSet.Tables.Add(table)

    ' Add ten rows.
    Dim newRow As DataRow
    Dim i As Integer
    For i = 0 To 9
        newRow = table.NewRow()
        newRow("item") = "item " & i.ToString()
        table.Rows.Add(newRow)
    Next i
    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.
    Dim xmlFilename As String = "XmlDocument.xml"
    Dim streamWrite As 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.
    Dim newDataSet As New DataSet("New DataSet")
       
    ' Read the XML document back in. 
    ' Create new FileStream to read schema with.
    Dim streamRead As 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")
End Sub
   
Private Sub PrintValues(dataSet As DataSet, label As String)
    Console.WriteLine(ControlChars.Cr & label)
    Dim table As DataTable
    Dim row As DataRow
    Dim column As DataColumn
    For Each table In  dataSet.Tables
        Console.WriteLine("TableName: " & table.TableName)         
        For Each row In  table.Rows             
            For Each column In  table.Columns
                Console.Write(ControlChars.Tab & " " & _
                   row(column).ToString())
            Next column
            Console.WriteLine()
        Next row
    Next table
End Sub

備註

方法提供一種方式,只會將資料或中的DataSet資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataSet into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請將mode參數設定WriteSchema為。To write both data and schema, set the mode parameter to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataSet,請ReadXml使用方法。To read XML data, or both schema and data into the DataSet, 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)

使用指定的 DataSetStream,寫入 XmlWriteMode 的目前資料,並選擇性寫入結構描述。Writes the current data, and optionally the schema, for the DataSet using the specified Stream and 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 物件。A Stream object used to write to a file.

mode
XmlWriteMode

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

備註

方法提供一種方式,只會將資料或中的DataSet資料和架構寫入WriteXmlSchema XML 檔,而方法只會寫入架構。 WriteXmlThe WriteXml method provides a way to write either data only, or both data and schema from a DataSet into an XML document, whereas the WriteXmlSchema method writes only the schema. 若要寫入資料和架構,請將mode參數設定WriteSchema為。To write both data and schema, set the mode parameter to WriteSchema.

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataSet,請ReadXml使用方法。To read XML data, or both schema and data into the DataSet, 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)

DataSet 的目前資料寫入指定的檔案。Writes the current data for the DataSet to 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

要寫入的目的檔案名稱 (包括路徑)。The file name (including the path) to which to write.

例外狀況

FileIOPermission 並不是設定為 WriteFileIOPermission is not set to Write.

備註

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

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataSet,請ReadXml使用方法。To read XML data, or both schema and data into the DataSet, 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.

安全性

FileIOPermission
用於讀取和寫入檔案。for reading and writing files. 相關聯的列舉:FileIOPermissionAccessAssociated enumeration: FileIOPermissionAccess

另請參閱

WriteXml(TextWriter)

使用指定的 DataSet,寫入 TextWriter 的目前資料。Writes the current data for the DataSet 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 物件。The TextWriter object with which to write.

備註

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

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataSet,請ReadXml使用方法。To read XML data, or both schema and data into the DataSet, 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)

DataSet 的目前資料寫入指定的 XmlWriterWrites the current data for the DataSet to 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

要用來寫入的 XmlWriterThe XmlWriter with which to write.

實作

備註

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

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataSet,請ReadXml使用方法。To read XML data, or both schema and data into the DataSet, 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)

使用指定的 DataSet,寫入 Stream 的目前資料。Writes the current data for the DataSet 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 物件。A Stream object used to write to a file.

範例

下列範例會建立System.IO.FileStream物件。The following example creates a System.IO.FileStream object. 然後,物件會與WriteXml方法搭配使用以寫入 XML 檔。The object is then used with the WriteXml method to write an XML document.

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);   
}
Private Sub WriteXmlToFile(thisDataSet As DataSet)
     If thisDataSet Is Nothing Then
         Return
     End If 

    ' Create a file name to write to.
     Dim filename As String = "XmlDoc.xml"

     ' Create the FileStream to write with.
     Dim stream As New System.IO.FileStream _
        (filename, System.IO.FileMode.Create)

     ' Write to the file with the WriteXml method.
     thisDataSet.WriteXml(stream)
End Sub

備註

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

請注意, ReadXmlReadXmlSchema方法的相同也是如此。Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. 若要在中讀取 XML 資料或架構和資料DataSet,請ReadXml使用方法。To read XML data, or both schema and data into the DataSet, 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.

另請參閱

適用於