Export (0) Print
Expand All

DataSet.WriteXml Method (TextWriter, XmlWriteMode)

.NET Framework 1.1

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 XMLWriteMode parameter to WriteSchema.

[Visual Basic]
Overloads Public Sub WriteXml( _
   ByVal writer As TextWriter, _
   ByVal mode As XmlWriteMode _
)
[C#]
public void WriteXml(
 TextWriter writer,
 XmlWriteMode mode
);
[C++]
public: void WriteXml(
 TextWriter* writer,
 XmlWriteMode mode
);
[JScript]
public function WriteXml(
   writer : TextWriter,
 mode : XmlWriteMode
);

Parameters

writer
A System.IO.TextWriter object used to write the document.
mode
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 WriteXmlMode 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.

Example

[Visual Basic, C#, C++] 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.

[Visual Basic] 
Private Sub DemonstrateReadWriteXMLDocumentWithStreamReader()
    ' Create a DataSet with one table and two columns.
    Dim OriginalDataSet As New DataSet("myDataSet")
    OriginalDataSet.Namespace = "NetFrameWork"
    Dim myTable As New DataTable("myTable")
    Dim c1 As New DataColumn("id", Type.GetType("System.Int32"))
    c1.AutoIncrement = True
    Dim c2 As New DataColumn("item")
    myTable.Columns.Add(c1)
    myTable.Columns.Add(c2)
    OriginalDataSet.Tables.Add(myTable)
    ' Add ten rows.
    Dim newRow As DataRow
    Dim i As Integer
    For i = 0 To 9
        newRow = myTable.NewRow()
        newRow("item") = "item " + i.ToString()
        myTable.Rows.Add(newRow)
    Next i
    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 StreamWriter.
    Dim xmlFilename As String = "myXmlDocument.xml"
    ' Create a new StreamWriter.
    Dim myStreamWriter As New System.IO.StreamWriter(xmlFilename)
    ' Use WriteXml to write the document.
    OriginalDataSet.WriteXml(myStreamWriter)
    ' Close the FileStream.
    myStreamWriter.Close()
       
    ' Dispose of the original DataSet.
    OriginalDataSet.Dispose()
    ' Create a new DataSet.
     Dim newDataSet As New DataSet("New DataSet")
       
    ' Create a StreamReader to read the file.
    Dim myStreamReader As New System.IO.StreamReader(xmlFilename)
    ' Read the XML document into the DataSet.
    newDataSet.ReadXml(myStreamReader)
    ' Close the StreamReader.
    myStreamReader.Close()
     
    ' Print out values of each table in the DataSet using the 
    ' function defined below.
    PrintValues(newDataSet, "New DataSet")
End Sub
   
   
Private Sub PrintValues(ds As DataSet, label As String)
    Console.WriteLine(ControlChars.Cr + label)
    Dim t As DataTable
    For Each t In  ds.Tables
        Console.WriteLine("TableName: " + t.TableName)
        Dim r As DataRow
        For Each r In  t.Rows
            Dim c As DataColumn
            For Each c In  t.Columns
                 Console.Write(ControlChars.Tab + " " + r(c).ToString())
            Next c
               Console.WriteLine()
           Next r
    Next t
End Sub

[C#] 
private void DemonstrateReadWriteXMLDocumentWithStreamReader(){
   // Create a DataSet with one table and two columns.
   DataSet OriginalDataSet = new DataSet("myDataSet");
    OriginalDataSet.Namespace= "NetFrameWork";
   DataTable myTable = new DataTable("myTable");
   DataColumn c1 = new DataColumn("id", Type.GetType("System.Int32"));
   c1.AutoIncrement= true;
   DataColumn c2 = new DataColumn("item");
   myTable.Columns.Add(c1);
   myTable.Columns.Add(c2);
   OriginalDataSet.Tables.Add(myTable);
   // Add ten rows.
   DataRow newRow;
   for(int i = 0; i < 10; i++){
      newRow = myTable.NewRow();
      newRow["item"]= "item " + i;
      myTable.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 StreamWriter.
   string xmlFilename = "myXmlDocument.xml";
   // Create a new StreamWriter.
   System.IO.StreamWriter myStreamWriter = 
      new System.IO.StreamWriter(xmlFilename);
   // Use WriteXml to write the document.
   OriginalDataSet.WriteXml(myStreamWriter);
   // Close the FileStream.
   myStreamWriter.Close();
      
   // Dispose of the original DataSet.
   OriginalDataSet.Dispose();
   // Create a new DataSet.
   DataSet newDataSet = new DataSet("New DataSet");
      
   // Create a StreamReader to read the file.
   System.IO.StreamReader myStreamReader = 
      new System.IO.StreamReader(xmlFilename);
   // Read the XML document into the DataSet.
   newDataSet.ReadXml(myStreamReader);
   // Close the StreamReader.
   myStreamReader.Close();

   // Print out values of each table in the DataSet using the 
   // function defined below.
   PrintValues(newDataSet,"New DataSet");
}

private void PrintValues(DataSet ds, string label){
   Console.WriteLine("\n" + label);
   foreach(DataTable t in ds.Tables){
      Console.WriteLine("TableName: " + t.TableName);
      foreach(DataRow r in t.Rows){
         foreach(DataColumn c in t.Columns){
            Console.Write("\t " + r[c] );
         }
         Console.WriteLine();
      }
   }
}

[C++] 
private:
 void DemonstrateReadWriteXMLDocumentWithStreamReader(){
    // Create a DataSet with one table and two columns.
    DataSet* OriginalDataSet = new DataSet(S"myDataSet");
     OriginalDataSet->Namespace= S"NetFrameWork";
    DataTable* myTable = new DataTable(S"myTable");
    DataColumn* c1 = new DataColumn(S"id", Type::GetType(S"System.Int32"));
    c1->AutoIncrement= true;
    DataColumn* c2 = new DataColumn(S"item");
    myTable->Columns->Add(c1);
    myTable->Columns->Add(c2);
    OriginalDataSet->Tables->Add(myTable);
    // Add ten rows.
    DataRow* newRow;
    for(int i = 0; i < 10; i++){
       newRow = myTable->NewRow();
       newRow->Item[S"item"]= String::Format( S"item {0}", __box(i));
       myTable->Rows->Add(newRow);
    }
    OriginalDataSet->AcceptChanges();
    // Print out values of each table in the DataSet using the 
    // function defined below.
    PrintValues(OriginalDataSet, S"Original DataSet");
    // Write the schema and data to XML file with StreamWriter.
    String* xmlFilename = S"myXmlDocument.xml";
    // Create a new StreamWriter.
    System::IO::StreamWriter* myStreamWriter = 
       new System::IO::StreamWriter(xmlFilename);
    // Use WriteXml to write the document.
    OriginalDataSet->WriteXml(myStreamWriter);
    // Close the FileStream.
    myStreamWriter->Close();
       
    // Dispose of the original DataSet.
    OriginalDataSet->Dispose();
    // Create a new DataSet.
    DataSet* newDataSet = new DataSet(S"New DataSet");
       
    // Create a StreamReader to read the file.
    System::IO::StreamReader* myStreamReader = 
       new System::IO::StreamReader(xmlFilename);
    // Read the XML document into the DataSet.
    newDataSet->ReadXml(myStreamReader);
    // Close the StreamReader.
    myStreamReader->Close();
 
    // Print out values of each table in the DataSet using the 
    // function defined below.
    PrintValues(newDataSet,S"New DataSet");
 }
 
 void PrintValues(DataSet* ds, String* label){
    Console::WriteLine(S"\n{0}", label);
    System::Collections::IEnumerator* myEnum = ds->Tables->GetEnumerator();
    while (myEnum->MoveNext())
    {
       DataTable* t = __try_cast<DataTable*>(myEnum->Current);
       Console::WriteLine(S"TableName: {0}", t->TableName);
       System::Collections::IEnumerator* myEnum1 = t->Rows->GetEnumerator();
       while (myEnum1->MoveNext())
       {
          DataRow* r = __try_cast<DataRow*>(myEnum1->Current);
          System::Collections::IEnumerator* myEnum2 = t->Columns->GetEnumerator();
          while (myEnum2->MoveNext())
          {
             DataColumn* c = __try_cast<DataColumn*>(myEnum2->Current);
             Console::Write(S"\t {0}", r->Item[c] );
          }
          Console::WriteLine();
       }
    }
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

DataSet Class | DataSet Members | System.Data Namespace | DataSet.WriteXml Overload List | ReadXml | ReadXmlSchema | WriteXml | WriteXmlSchema

Show:
© 2014 Microsoft