.NET Framework Class Library  

DataSet.ReadXml Method

Reads XML schema and data into the DataSet.

Overload List

Reads XML schema and data into the DataSet using the specified System.IO.Stream.

[Visual Basic] Overloads Public Function ReadXml(Stream) As XmlReadMode
[C#] public XmlReadMode ReadXml(Stream);
[C++] public: XmlReadMode ReadXml(Stream*);
[JScript] public function ReadXml(Stream) : XmlReadMode;

Reads XML schema and data into the DataSet using the specified file.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function ReadXml(String) As XmlReadMode
[C#] public XmlReadMode ReadXml(string);
[C++] public: XmlReadMode ReadXml(String*);
[JScript] public function ReadXml(String) : XmlReadMode;

Reads XML schema and data into the DataSet using the specified System.IO.TextReader.

[Visual Basic] Overloads Public Function ReadXml(TextReader) As XmlReadMode
[C#] public XmlReadMode ReadXml(TextReader);
[C++] public: XmlReadMode ReadXml(TextReader*);
[JScript] public function ReadXml(TextReader) : XmlReadMode;

Reads XML schema and data into the DataSet using the specified System.Xml.XmlReader.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function ReadXml(XmlReader) As XmlReadMode
[C#] public XmlReadMode ReadXml(XmlReader);
[C++] public: XmlReadMode ReadXml(XmlReader*);
[JScript] public function ReadXml(XmlReader) : XmlReadMode;

Reads XML schema and data into the DataSet using the specified System.IO.Stream and XmlReadMode.

[Visual Basic] Overloads Public Function ReadXml(Stream, XmlReadMode) As XmlReadMode
[C#] public XmlReadMode ReadXml(Stream, XmlReadMode);
[C++] public: XmlReadMode ReadXml(Stream*, XmlReadMode);
[JScript] public function ReadXml(Stream, XmlReadMode) : XmlReadMode;

Reads XML schema and data into the DataSet using the specified file and XmlReadMode.

[Visual Basic] Overloads Public Function ReadXml(String, XmlReadMode) As XmlReadMode
[C#] public XmlReadMode ReadXml(string, XmlReadMode);
[C++] public: XmlReadMode ReadXml(String*, XmlReadMode);
[JScript] public function ReadXml(String, XmlReadMode) : XmlReadMode;

Reads XML schema and data into the DataSet using the specified System.IO.TextReader and XmlReadMode.

[Visual Basic] Overloads Public Function ReadXml(TextReader, XmlReadMode) As XmlReadMode
[C#] public XmlReadMode ReadXml(TextReader, XmlReadMode);
[C++] public: XmlReadMode ReadXml(TextReader*, XmlReadMode);
[JScript] public function ReadXml(TextReader, XmlReadMode) : XmlReadMode;

Reads XML schema and data into the DataSet using the specified System.Xml.XmlReader and XmlReadMode.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function ReadXml(XmlReader, XmlReadMode) As XmlReadMode
[C#] public XmlReadMode ReadXml(XmlReader, XmlReadMode);
[C++] public: XmlReadMode ReadXml(XmlReader*, XmlReadMode);
[JScript] public function ReadXml(XmlReader, XmlReadMode) : XmlReadMode;

Example

[Visual Basic, 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, C#] Note   This example shows how to use one of the overloaded versions of ReadXml. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Private Sub DemonstrateReadWriteXMLDocumentWithXMLReader()
    ' 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 XML schema and data to file with FileStream.
    Dim xmlFilename As String = "myXmlDocument.xml"
    ' Create FileStream    
    Dim fsWriteXml As New System.IO.FileStream _
       (xmlFilename, System.IO.FileMode.Create)
    ' Create an XmlTextWriter to write the file.
    Dim xmlWriter As New System.Xml.XmlTextWriter _
       (fsWriteXml, System.Text.Encoding.Unicode)
    ' Use WriteXml to write the document.
    OriginalDataSet.WriteXml(xmlWriter)
    ' Close the FileStream.
    fsWriteXml.Close()
       
    ' Dispose of the original DataSet.
    OriginalDataSet.Dispose()
    ' Create a new DataSet.
    Dim newDataSet As New DataSet("New DataSet")
     
    ' Read the XML document back in. 
    ' Create new FileStream to read schema with.
    Dim fsReadXml As New System.IO.FileStream _
       (xmlFilename, System.IO.FileMode.Open)
    ' Create an XmlTextReader to read the file.
    Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml)
    ' Read the XML document into the DataSet.
    newDataSet.ReadXml(myXmlReader)
    ' Close the XmlTextReader
    myXmlReader.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
    Dim r As DataRow
    Dim c As DataColumn
    For Each t In  ds.Tables
        Console.WriteLine("TableName: " + t.TableName)
        For Each r In  t.Rows
            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 DemonstrateReadWriteXMLDocumentWithXMLReader(){
   // 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 XML schema and data to file with FileStream.
   string xmlFilename = "myXmlDocument.xml";
   // Create FileStream    
   System.IO.FileStream fsWriteXml = new System.IO.FileStream
      (xmlFilename, System.IO.FileMode.Create);
   // Create an XmlTextWriter to write the file.
   System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter
      (fsWriteXml, System.Text.Encoding.Unicode);
   // Use WriteXml to write the document.
   OriginalDataSet.WriteXml(xmlWriter);
   // Close the FileStream.
   fsWriteXml.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 fsReadXml = new System.IO.FileStream
      (xmlFilename, System.IO.FileMode.Open);
   // Create an XmlTextReader to read the file.
   System.Xml.XmlTextReader myXmlReader = 
      new System.Xml.XmlTextReader(fsReadXml);
   // Read the XML document into the DataSet.
   newDataSet.ReadXml(myXmlReader);
   // Close the XmlTextReader
   myXmlReader.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++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

DataSet Class | DataSet Members | System.Data Namespace