This documentation is archived and is not being maintained.

DataSet.ReadXml Method (XmlReader)

.NET Framework 1.1

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

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

Parameters

reader
The XmlReader from which to read.

Return Value

The XmlReadMode used to read the data.

Remarks

The ReadXml method provides a way to read either data only, or both data and schema into a DataSet from an XML document, whereas the ReadXmlSchema method reads only the schema. To read both data and schema, use one of the ReadXML overloads that includes the XmlReadMode parameter, and set its value to ReadSchema.

Note that the same is true for the WriteXml and WriteXmlSchema methods, respectively. To write XML data, or both schema and data from the DataSet, use the WriteXml method. To write just the schema, use the WriteXmlSchema method.

If an in-line schema is specified, the in-line schema is used to extend the existing relational structure prior to loading the data. If there are any conflicts (for example, the same column in the same table defined with different datatypes) an exception is raised.

If no in-line schema is specified, the relational structure is extended through inference, as necessary, according to the structure of the XML document. If the schema cannot be extended through inference in order to expose all data, an exception is raised.

If the XSD schema for a DataSet includes a targetNamespace, data may not be read, and you may encounter exceptions when calling ReadXml to load the DataSet with XML that contains elements with no qualifying namespace. To read unqualified elements, set elementFormDefault equal to "qualified" in your XSD schema as the following example demonstrates.

<xsd:schema id="MyDataSet" 
   elementFormDefault="qualified" 
   targetNamespace="http://www.tempuri.org/MyDataSet.xsd" 
   xmlns="http://www.tempuri.org/MyDataSet.xsd" 
   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
   xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
</xsd:schema>

System.Xml.XmlTextReader inherits from XmlReader.

Note   If the schema for your DataSet contains elements of the same name, but different type, in the same namespace, an exception is thrown when you attempt to read the schema into the DataSet with ReadXml by specifying XmlReadMode.ReadSchema. This exception does not occur if you are using .NET Framework version 1.0.

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 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++] 
private:
 void DemonstrateReadWriteXMLDocumentWithXMLReader(){
    // 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 XML schema and data to file with FileStream.
    String* xmlFilename = S"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(S"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,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, .NET Compact Framework

See Also

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

Show: