DataTable.WriteXml Method

Definition

Writes the current contents of the DataTable as XML.

Overloads

WriteXml(TextWriter, Boolean)

Writes the current contents of the DataTable as XML using the specified TextWriter. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(TextWriter, XmlWriteMode)

Writes the current data, and optionally the schema, for the DataTable using the specified TextWriter 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 DataTable using the specified file 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 DataTable to the specified file using the specified XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema.

WriteXml(XmlWriter, Boolean)

Writes the current contents of the DataTable as XML using the specified XmlWriter.

WriteXml(XmlWriter, XmlWriteMode)

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

WriteXml(Stream, XmlWriteMode, Boolean)

Writes the current data, and optionally the schema, for the DataTable to the specified file using the specified XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(TextWriter, XmlWriteMode, Boolean)

Writes the current data, and optionally the schema, for the DataTable using the specified TextWriter and XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(String, Boolean)

Writes the current contents of the DataTable as XML using the specified file. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(Stream, Boolean)

Writes the current contents of the DataTable as XML using the specified Stream. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(XmlWriter, XmlWriteMode, Boolean)

Writes the current data, and optionally the schema, for the DataTable using the specified XmlWriter and XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(String)

Writes the current contents of the DataTable as XML using the specified file.

WriteXml(TextWriter)

Writes the current contents of the DataTable as XML using the specified TextWriter.

WriteXml(Stream)

Writes the current contents of the DataTable as XML using the specified Stream.

WriteXml(String, XmlWriteMode, Boolean)

Writes the current data, and optionally the schema, for the DataTable using the specified file and XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

WriteXml(XmlWriter)

Writes the current contents of the DataTable as XML using the specified XmlWriter.

Examples

The following console application creates two DataTable instances, adds each to a DataSet, creates a DataRelation relating the two tables, and then uses the WriteXml method to write the data contained within the parent table to a TextWriter. The example demonstrates the behavior when setting the writeHierarchy parameter to each of its values.

Note

This example shows how to use one of the overloaded versions of WriteXml. For other examples that might be available, see the individual overload topics.

static void Main()
{
    DataSet ds = new DataSet();
    DataTable customerTable = GetCustomers();
    DataTable orderTable = GetOrders();

    ds.Tables.Add(customerTable);
    ds.Tables.Add(orderTable);
    ds.Relations.Add("CustomerOrder",
        new DataColumn[] { customerTable.Columns[0] },
        new DataColumn[] { orderTable.Columns[1] }, true);

    System.IO.StringWriter writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, false);
    PrintOutput(writer, "Customer table, without hierarchy");

    writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, true);
    PrintOutput(writer, "Customer table, with hierarchy");

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static DataTable GetCustomers()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(System.Int32));
    table.Columns.Add("Name", typeof(System.String));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    table.AcceptChanges();
    return table;
}

private static DataTable GetOrders()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create three columns; OrderID, CustomerID, and OrderDate.
    table.Columns.Add(new DataColumn("OrderID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("CustomerID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("OrderDate", typeof(System.DateTime)));

    // Set the OrderID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { table.Columns[0] };

    table.Rows.Add(new object[] { 1, 1, "12/2/2003" });
    table.Rows.Add(new object[] { 2, 1, "1/3/2004" });
    table.Rows.Add(new object[] { 3, 2, "11/13/2004" });
    table.Rows.Add(new object[] { 4, 3, "5/16/2004" });
    table.Rows.Add(new object[] { 5, 3, "5/22/2004" });
    table.Rows.Add(new object[] { 6, 4, "6/15/2004" });
    table.AcceptChanges();
    return table;
}

private static void PrintOutput(System.IO.TextWriter writer, string caption)
{
    Console.WriteLine("==============================");
    Console.WriteLine(caption);
    Console.WriteLine("==============================");
    Console.WriteLine(writer.ToString());
}

The example displays the following output in the console window:

==============================
Customer table, without hierarchy
==============================
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema
" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table1">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:int" />
                <xs:element name="Name" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">
        <xs:selector xpath=".//Table1" />
        <xs:field xpath="ID" />
      </xs:unique>
    </xs:element>
  </xs:schema>
  <Table1>
    <ID>1</ID>
    <Name>Mary</Name>
  </Table1>
  <Table1>
    <ID>2</ID>
    <Name>Andy</Name>
  </Table1>
  <Table1>
    <ID>3</ID>
    <Name>Peter</Name>
  </Table1>
  <Table1>
    <ID>4</ID>
    <Name>Russ</Name>
  </Table1>
</NewDataSet>
==============================
Customer table, with hierarchy
==============================
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema
" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table1">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:int" />
                <xs:element name="Name" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="Table2">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="OrderID" type="xs:int" />
                <xs:element name="CustomerID" type="xs:int" minOccurs="0" />
                <xs:element name="OrderDate" type="xs:dateTime" minOccurs="0" />

              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">
        <xs:selector xpath=".//Table1" />
        <xs:field xpath="ID" />
      </xs:unique>
      <xs:unique name="Table2_Constraint1" msdata:ConstraintName="Constraint1" m
sdata:PrimaryKey="true">
        <xs:selector xpath=".//Table2" />
        <xs:field xpath="OrderID" />
      </xs:unique>
      <xs:keyref name="CustomerOrder" refer="Constraint1">
        <xs:selector xpath=".//Table2" />
        <xs:field xpath="CustomerID" />
      </xs:keyref>
    </xs:element>
  </xs:schema>
  <Table1>
    <ID>1</ID>
    <Name>Mary</Name>
  </Table1>
  <Table1>
    <ID>2</ID>
    <Name>Andy</Name>
  </Table1>
  <Table1>
    <ID>3</ID>
    <Name>Peter</Name>
  </Table1>
  <Table1>
    <ID>4</ID>
    <Name>Russ</Name>
  </Table1>
  <Table2>
    <OrderID>1</OrderID>
    <CustomerID>1</CustomerID>
    <OrderDate>2003-12-02T00:00:00.0000000-08:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>2</OrderID>
    <CustomerID>1</CustomerID>
    <OrderDate>2004-01-03T00:00:00.0000000-08:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>3</OrderID>
    <CustomerID>2</CustomerID>
    <OrderDate>2004-11-13T00:00:00.0000000-08:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>4</OrderID>
    <CustomerID>3</CustomerID>
    <OrderDate>2004-05-16T00:00:00.0000000-07:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>5</OrderID>
    <CustomerID>3</CustomerID>
    <OrderDate>2004-05-22T00:00:00.0000000-07:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>6</OrderID>
    <CustomerID>4</CustomerID>
    <OrderDate>2004-06-15T00:00:00.0000000-07:00</OrderDate>
  </Table2>
</NewDataSet>

Remarks

The WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. To write both data and schema, use one of the overloads that include the XmlWriteMode 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 DataTable, 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.

WriteXml(TextWriter, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current contents of the DataTable as XML using the specified TextWriter. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

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

Parameters

writer
TextWriter

The TextWriter with which to write the content.

writeHierarchy
Boolean

If true, write the contents of the current table and all its descendants. If false (the default value), write the data for the current table only.

Examples

The following console application creates two DataTable instances, adds each to a DataSet, creates a DataRelation relating the two tables, and then uses the WriteXml method to write the data contained within the parent table to a TextWriter. The example demonstrates the behavior when setting the writeHierarchy parameter to true.

static void Main()
{
    DataSet ds = new DataSet();
    DataTable customerTable = GetCustomers();
    DataTable orderTable = GetOrders();

    ds.Tables.Add(customerTable);
    ds.Tables.Add(orderTable);
    ds.Relations.Add("CustomerOrder",
        new DataColumn[] { customerTable.Columns[0] },
        new DataColumn[] { orderTable.Columns[1] }, true);

    System.IO.StringWriter writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, true);
    PrintOutput(writer, "Customer table, with hierarchy");

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static DataTable GetCustomers()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(System.Int32));
    table.Columns.Add("Name", typeof(System.String));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    table.AcceptChanges();
    return table;
}

private static DataTable GetOrders()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create three columns; OrderID, CustomerID, and OrderDate.
    table.Columns.Add(new DataColumn("OrderID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("CustomerID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("OrderDate", typeof(System.DateTime)));

    // Set the OrderID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { table.Columns[0] };

    table.Rows.Add(new object[] { 1, 1, "12/2/2003" });
    table.Rows.Add(new object[] { 2, 1, "1/3/2004" });
    table.Rows.Add(new object[] { 3, 2, "11/13/2004" });
    table.Rows.Add(new object[] { 4, 3, "5/16/2004" });
    table.Rows.Add(new object[] { 5, 3, "5/22/2004" });
    table.Rows.Add(new object[] { 6, 4, "6/15/2004" });
    table.AcceptChanges();
    return table;
}

private static void PrintOutput(System.IO.TextWriter stream,
    string caption)
{
    Console.WriteLine("==============================");
    Console.WriteLine(caption);
    Console.WriteLine("==============================");
    Console.WriteLine(stream.ToString());
}

Remarks

The WriteXml method provides a way to write either data only, or both data and schema from a DataTable 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 XmlWriteMode parameter, and set its value to WriteSchema.

Normally the WriteXml method writes the data only for the current table. To write the data for the current table and all its descendant, related tables, call the method with the writeHierarchy parameter set to true.

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 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:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current data, and optionally the schema, for the DataTable 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

The TextWriter used to write the document.

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 DataTable 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 XmlWriteMode 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 DataTable, 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 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:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current data, and optionally the schema, for the DataTable using the specified file and 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 name of the file to which the data will be written.

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 DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. To write both data and schema, use one of the overloads that include the XmlWriteMode 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 DataTable, 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 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:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current data, and optionally the schema, for the DataTable to the specified file using the specified 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

The stream to which the data will be written.

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 DataTable 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 XmlWriteMode 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 DataTable, 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 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, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current contents of the DataTable as XML using the specified XmlWriter.

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

Parameters

writer
XmlWriter

The XmlWriter with which to write the contents.

writeHierarchy
Boolean

If true, write the contents of the current table and all its descendants. If false (the default value), write the data for the current table only.

Remarks

The WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. To write both data and schema, use one of the overloads that include the XmlWriteMode 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 DataTable, use the ReadXml method. To read just the schema, use the ReadXmlSchema method.

Normally the WriteXml method writes the data only for the current table. To write the data for the current table and its entire descendant, related tables, call the method with the writeHierarchy parameter set to true.

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 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, XmlWriteMode)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current data, and optionally the schema, for the DataTable 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 used to write the document.

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 DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. To write both data and schema, use one of the overloads that include the XmlWriteMode 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 DataTable, 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 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, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current data, and optionally the schema, for the DataTable to the specified file using the specified XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

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

Parameters

stream
Stream

The stream to which the data will be written.

mode
XmlWriteMode

One of the XmlWriteMode values.

writeHierarchy
Boolean

If true, write the contents of the current table and all its descendants. If false (the default value), write the data for the current table only.

Remarks

The WriteXml method provides a way to write either data only, or both data and schema from a DataTable 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 XmlWriteMode 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 DataTable, use the ReadXml method. To read just the schema, use the ReadXmlSchema method.

Normally, the WriteXml method saves data only for the current table. The WriteXml method provides a way to write either data only, or both data and schema from a DataTable 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 XmlWriteMode 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 DataTable, use the ReadXml method. To read just the schema, use the ReadXmlSchema method.

Normally the WriteXml method writes the data only for the current table. To write the data for the current table and all of its descendant, related tables, call the method with the writeHierarchy parameter set to true.

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 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, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current data, and optionally the schema, for the DataTable using the specified TextWriter and XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

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

Parameters

writer
TextWriter

The TextWriter used to write the document.

mode
XmlWriteMode

One of the XmlWriteMode values.

writeHierarchy
Boolean

If true, write the contents of the current table and all its descendants. If false (the default value), write the data for the current table only.

Examples

The following console application creates two DataTable instances, adds each to a DataSet, creates a DataRelation relating the two tables, and then uses the WriteXml method to write the data contained within the parent table to a TextWriter. The example demonstrates the behavior when setting the writeHierarchy parameter to each of its values.

static void Main()
{
    DataSet ds = new DataSet();
    DataTable customerTable = GetCustomers();
    DataTable orderTable = GetOrders();

    ds.Tables.Add(customerTable);
    ds.Tables.Add(orderTable);
    ds.Relations.Add("CustomerOrder",
        new DataColumn[] { customerTable.Columns[0] },
        new DataColumn[] { orderTable.Columns[1] }, true);

    System.IO.StringWriter writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, false);
    PrintOutput(writer, "Customer table, without hierarchy");

    writer = new System.IO.StringWriter();
    customerTable.WriteXml(writer, XmlWriteMode.WriteSchema, true);
    PrintOutput(writer, "Customer table, with hierarchy");

    Console.WriteLine("Press any key to continue.");
    Console.ReadKey();
}

private static DataTable GetCustomers()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create two columns, ID and Name.
    DataColumn idColumn = table.Columns.Add("ID", typeof(System.Int32));
    table.Columns.Add("Name", typeof(System.String));

    // Set the ID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { idColumn };

    table.Rows.Add(new object[] { 1, "Mary" });
    table.Rows.Add(new object[] { 2, "Andy" });
    table.Rows.Add(new object[] { 3, "Peter" });
    table.Rows.Add(new object[] { 4, "Russ" });
    table.AcceptChanges();
    return table;
}

private static DataTable GetOrders()
{
    // Create sample Customers table, in order
    // to demonstrate the behavior of the DataTableReader.
    DataTable table = new DataTable();

    // Create three columns; OrderID, CustomerID, and OrderDate.
    table.Columns.Add(new DataColumn("OrderID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("CustomerID", typeof(System.Int32)));
    table.Columns.Add(new DataColumn("OrderDate", typeof(System.DateTime)));

    // Set the OrderID column as the primary key column.
    table.PrimaryKey = new DataColumn[] { table.Columns[0] };

    table.Rows.Add(new object[] { 1, 1, "12/2/2003" });
    table.Rows.Add(new object[] { 2, 1, "1/3/2004" });
    table.Rows.Add(new object[] { 3, 2, "11/13/2004" });
    table.Rows.Add(new object[] { 4, 3, "5/16/2004" });
    table.Rows.Add(new object[] { 5, 3, "5/22/2004" });
    table.Rows.Add(new object[] { 6, 4, "6/15/2004" });
    table.AcceptChanges();
    return table;
}

private static void PrintOutput(System.IO.TextWriter writer,
    string caption)
{
    Console.WriteLine("==============================");
    Console.WriteLine(caption);
    Console.WriteLine("==============================");
    Console.WriteLine(writer.ToString());
}

The example displays the following output in the console window:

==============================
Customer table, without hierarchy
==============================
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema
" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table1">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:int" />
                <xs:element name="Name" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">
        <xs:selector xpath=".//Table1" />
        <xs:field xpath="ID" />
      </xs:unique>
    </xs:element>
  </xs:schema>
  <Table1>
    <ID>1</ID>
    <Name>Mary</Name>
  </Table1>
  <Table1>
    <ID>2</ID>
    <Name>Andy</Name>
  </Table1>
  <Table1>
    <ID>3</ID>
    <Name>Peter</Name>
  </Table1>
  <Table1>
    <ID>4</ID>
    <Name>Russ</Name>
  </Table1>
</NewDataSet>
==============================
Customer table, with hierarchy
==============================
<NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema
" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table1">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table1">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="ID" type="xs:int" />
                <xs:element name="Name" type="xs:string" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="Table2">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="OrderID" type="xs:int" />
                <xs:element name="CustomerID" type="xs:int" minOccurs="0" />
                <xs:element name="OrderDate" type="xs:dateTime" minOccurs="0" />

              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
      <xs:unique name="Constraint1" msdata:PrimaryKey="true">
        <xs:selector xpath=".//Table1" />
        <xs:field xpath="ID" />
      </xs:unique>
      <xs:unique name="Table2_Constraint1" msdata:ConstraintName="Constraint1" m
sdata:PrimaryKey="true">
        <xs:selector xpath=".//Table2" />
        <xs:field xpath="OrderID" />
      </xs:unique>
      <xs:keyref name="CustomerOrder" refer="Constraint1">
        <xs:selector xpath=".//Table2" />
        <xs:field xpath="CustomerID" />
      </xs:keyref>
    </xs:element>
  </xs:schema>
  <Table1>
    <ID>1</ID>
    <Name>Mary</Name>
  </Table1>
  <Table1>
    <ID>2</ID>
    <Name>Andy</Name>
  </Table1>
  <Table1>
    <ID>3</ID>
    <Name>Peter</Name>
  </Table1>
  <Table1>
    <ID>4</ID>
    <Name>Russ</Name>
  </Table1>
  <Table2>
    <OrderID>1</OrderID>
    <CustomerID>1</CustomerID>
    <OrderDate>2003-12-02T00:00:00.0000000-08:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>2</OrderID>
    <CustomerID>1</CustomerID>
    <OrderDate>2004-01-03T00:00:00.0000000-08:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>3</OrderID>
    <CustomerID>2</CustomerID>
    <OrderDate>2004-11-13T00:00:00.0000000-08:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>4</OrderID>
    <CustomerID>3</CustomerID>
    <OrderDate>2004-05-16T00:00:00.0000000-07:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>5</OrderID>
    <CustomerID>3</CustomerID>
    <OrderDate>2004-05-22T00:00:00.0000000-07:00</OrderDate>
  </Table2>
  <Table2>
    <OrderID>6</OrderID>
    <CustomerID>4</CustomerID>
    <OrderDate>2004-06-15T00:00:00.0000000-07:00</OrderDate>
  </Table2>
</NewDataSet>

Remarks

The WriteXml method provides a way to write either data only, or both data and schema from a DataTable 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 XmlWriteMode 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 DataTable, use the ReadXml method. To read just the schema, use the ReadXmlSchema method.

Normally, the WriteXml method saves data only for the current table. The WriteXml method provides a way to write either data only, or both data and schema from a DataTable 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 XmlWriteMode 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 DataTable, use the ReadXml method. To read just the schema, use the ReadXmlSchema method.

Normally the WriteXml method writes the data only for the current table. To write the data for the current table and all of its descendant, related tables, call the method with the writeHierarchy parameter set to true.

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 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, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current contents of the DataTable as XML using the specified file. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

public void WriteXml(string fileName, bool writeHierarchy);

Parameters

fileName
String

The file to which to write the XML data.

writeHierarchy
Boolean

If true, write the contents of the current table and all its descendants. If false (the default value), write the data for the current table only.

Remarks

The WriteXml method provides a way to write either data only, or both data and schema from a DataTable 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 XmlWriteMode 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 DataTable, use the ReadXml method. To read just the schema, use the ReadXmlSchema method.

Normally the WriteXml method writes the data only for the current table. To write the data for the current table and all of its descendant, related tables, call the method with the writeHierarchy parameter set to true.

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 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, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current contents of the DataTable as XML using the specified Stream. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

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

Parameters

stream
Stream

The stream to which the data will be written.

writeHierarchy
Boolean

If true, write the contents of the current table and all its descendants. If false (the default value), write the data for the current table only.

Remarks

Use the WriteXmlSchema method to write the schema for a DataTable to an XML document. The schema includes table, relation, and constraint definitions.

The XML schema is written using the XSD standard.

To write the data to an XML document, use the WriteXml method.

Normally the WriteXml method writes the data only for the current table. To write the data for the current table and all of its descendant, related tables, call the method with the writeHierarchy parameter set to true.

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 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, XmlWriteMode, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current data, and optionally the schema, for the DataTable using the specified XmlWriter and XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

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

Parameters

writer
XmlWriter

The XmlWriter used to write the document.

mode
XmlWriteMode

One of the XmlWriteMode values.

writeHierarchy
Boolean

If true, write the contents of the current table and all its descendants. If false (the default value), write the data for the current table only.

Remarks

The WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. To write both data and schema, use one of the overloads that include the XmlWriteMode 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 DataTable, use the ReadXml method. To read just the schema, use the ReadXmlSchema method.

Normally the WriteXml method writes the data only for the current table. To write the data for the current table and its entire descendant, related tables, call the method with the writeHierarchy parameter set to true.

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 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:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current contents of the DataTable as XML using the specified file.

public void WriteXml(string fileName);

Parameters

fileName
String

The file to which to write the XML data.

Remarks

The WriteXml method provides a way to write either data only, or both data and schema from a DataTable 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 XmlWriteMode 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 DataTable, 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 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:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current contents of the DataTable as XML using the specified TextWriter.

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

Parameters

writer
TextWriter

The TextWriter with which to write the content.

Remarks

The WriteXml method provides a way to write either data only, or both data and schema from a DataTable 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 XmlWriteMode 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 DataTable, 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 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:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current contents of the DataTable as XML using the specified Stream.

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

Parameters

stream
Stream

The stream to which the data will be written.

Remarks

The WriteXml method provides a way to write either data only, or both data and schema from a DataTable 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 XmlWriteMode 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 DataTable, 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 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, Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current data, and optionally the schema, for the DataTable using the specified file and XmlWriteMode. To write the schema, set the value for the mode parameter to WriteSchema. To save the data for the table and all its descendants, set the writeHierarchy parameter to true.

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

Parameters

fileName
String

The name of the file to which the data will be written.

mode
XmlWriteMode

One of the XmlWriteMode values.

writeHierarchy
Boolean

If true, write the contents of the current table and all its descendants. If false (the default value), write the data for the current table only.

Remarks

The WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. To write both data and schema, use one of the overloads that include the XmlWriteMode 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 DataTable, use the ReadXml method. To read just the schema, use the ReadXmlSchema method.

Normally, the WriteXml method saves data only for the current table. If you want to save the data for the current table and all of schema, the WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. To write both data and schema, use one of the overloads that include the XmlWriteMode 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 DataTable, use the ReadXml method. To read just the schema, use the ReadXmlSchema method.

Normally the WriteXml method writes the data only for the current table. To write the data for the current table and all descendant, related tables, call the method with the writeHierarchy parameter set to true.

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 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:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Writes the current contents of the DataTable as XML using 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 the contents.

Remarks

The WriteXml method provides a way to write either data only, or both data and schema from a DataTable into an XML document, whereas the WriteXmlSchema method writes only the schema. To write both data and schema, use one of the overloads that include the XmlWriteMode 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 DataTable, 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 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