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 _
Dim instance As DataSet 
Dim writer As TextWriter 
Dim mode As XmlWriteMode

instance.WriteXml(writer, mode)


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.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0