This documentation is archived and is not being maintained.

DataSet.WriteXml Method (TextWriter, XmlWriteMode)

Writes the current data, and optionally the schema, for the DataSet using the specified TextWriter and XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema.

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

Public Sub WriteXml ( _
	writer As TextWriter, _
	mode As XmlWriteMode _


Type: System.IO.TextWriter
A System.IO.TextWriter object used to write the document.
Type: System.Data.XmlWriteMode
One of the XmlWriteMode values.

The 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. To write both data and schema, set the mode parameter to WriteSchema.

Note that the same is true for the ReadXml and ReadXmlSchema methods, respectively. To read XML data, or both schema and data into the DataSet, use the ReadXml method. To read just the schema, use the ReadXmlSchema method.


An InvalidOperationException will be thrown if a column type in the DataRow being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.

The following example first creates a simple DataSet with one DataTable, two columns, and ten rows. The DataSet schema and data are written to disk by invoking the WriteXml method. A second DataSet is created and the ReadXml method is used to fill it with schema and data.

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", _
    idColumn.AutoIncrement = True

    Dim itemColumn As New DataColumn("item")

    ' Add ten rows.
    Dim newRow As DataRow
    Dim i As Integer
    For i = 0 To 9
        newRow = table.NewRow()
        newRow("item") = "item " & i.ToString()
    Next i

    ' 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.

    ' Close the FileStream.

    ' Dispose of the original DataSet.
    ' 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)

    ' 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 & " " & _
            Next column
        Next row
    Next table
End Sub

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.