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

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

member WriteXml : 
        writer:TextWriter * 
        mode:XmlWriteMode * 
        writeHierarchy:bool -> unit

Parameters

writer
Type: System.IO.TextWriter

The TextWriter used to write the document.

mode
Type: System.Data.XmlWriteMode

One of the XmlWriteMode values.

writeHierarchy
Type: System.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.

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.

NoteNote

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

No code example is currently available or this language may not be supported.

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>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft