Export (0) Print
Expand All

DataTable.WriteXml Method (TextWriter, Boolean)

Note: This method is new in the .NET Framework version 2.0.

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.

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

public void WriteXml (
	TextWriter writer,
	bool writeHierarchy
)
public void WriteXml (
	TextWriter writer, 
	boolean writeHierarchy
)
public function WriteXml (
	writer : TextWriter, 
	writeHierarchy : boolean
)

Parameters

writer

The TextWriter with which to write the content.

writeHierarchy

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.

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.

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


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft