DataSet.WriteXml Method

Definition

Writes XML data, and optionally the schema, from the DataSet.

Overloads

WriteXml(XmlWriter, XmlWriteMode)

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

WriteXml(String, XmlWriteMode)

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

WriteXml(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.

WriteXml(Stream, XmlWriteMode)

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

WriteXml(String)

Writes the current data for the DataSet to the specified file.

WriteXml(TextWriter)

Writes the current data for the DataSet using the specified TextWriter.

WriteXml(XmlWriter)

Writes the current data for the DataSet to the specified XmlWriter.

WriteXml(Stream)

Writes the current data for the DataSet using the specified Stream.

WriteXml(XmlWriter, XmlWriteMode)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

Writes the current data, and optionally the schema, for the DataSet using the specified XmlWriter and XmlWriteMode. To 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);

Parameters

writer
XmlWriter

The XmlWriter with which to write.

mode
XmlWriteMode

One of the XmlWriteMode values.

Examples

The following example creates a System.IO.FileStream object that is used to create a new XmlTextWriter. 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();
}

Remarks

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.

Note

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.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)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

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

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

Parameters

fileName
String

The file name (including the path) to which to write.

mode
XmlWriteMode

One of the XmlWriteMode values.

Exceptions

Examples

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);
}

Remarks

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.

Note

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.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)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

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.

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

Parameters

writer
TextWriter

A TextWriter object used to write the document.

mode
XmlWriteMode

One of the XmlWriteMode values.

Examples

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 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();
        }
    }
}

Remarks

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.

Note

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.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)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

Writes the current data, and optionally the schema, for the DataSet using the specified Stream and XmlWriteMode. To 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);

Parameters

stream
Stream

A Stream object used to write to a file.

mode
XmlWriteMode

One of the XmlWriteMode values.

Remarks

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.

Note

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.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)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

Writes the current data for the DataSet to the specified file.

public void WriteXml(string fileName);

Parameters

fileName
String

The file name (including the path) to which to write.

Exceptions

Remarks

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, use one of the overloads that includes the mode parameter, and set its value 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.

Note

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.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)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

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);

Parameters

writer
TextWriter

The TextWriter object with which to write.

Remarks

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, use one of the overloads that includes the mode parameter, and set its value 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.

Note

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.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)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

Writes the current data for the DataSet to the specified XmlWriter.

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

Parameters

writer
XmlWriter

The XmlWriter with which to write.

Remarks

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, use one of the overloads that includes the mode parameter, and set its value 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.

Note

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.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)

Source:
DataSet.cs
Source:
DataSet.cs
Source:
DataSet.cs

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);

Parameters

stream
Stream

A Stream object used to write to a file.

Examples

The following example creates a System.IO.FileStream object. 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);
}

Remarks

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, use one of the overloads that includes the mode parameter, and set its value 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.

Note

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.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