Export (0) Print
Expand All

DataSet.ReadXml Method (String)

.NET Framework 1.1

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

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

Parameters

fileName
The filename (including the path) from which to read.

Return Value

The XmlReadMode used to read the data.

Exceptions

Exception Type Condition
SecurityException FileIOPermission is not set to Read.

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

.NET Compact Framework Platform Note:  Relative paths are not supported when using the file:// URI scheme. Specify absolute path information.

.NET Compact Framework Platform Note:  Relative paths are not supported when using the file:// URI scheme. Specify absolute path information.

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 an XML file.
    Dim xmlFilename As String = "myXmlDocument.xml"
    ' Use WriteXml to write the document.
    OriginalDataSet.WriteXml(xmlFilename)
    ' Dispose of the original DataSet.
    OriginalDataSet.Dispose()
    ' Create a new DataSet.
    Dim newDataSet As New DataSet("New DataSet")
    ' Read the XML document into the DataSet.
    newDataSet.ReadXml(xmlFilename)
    ' 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 an XML file.
   string xmlFilename = "myXmlDocument.xml";
   // Use WriteXml to write the document.
   OriginalDataSet.WriteXml(xmlFilename);
   // Dispose of the original DataSet.
   OriginalDataSet.Dispose();
   // Create a new DataSet.
   DataSet newDataSet = new DataSet("New DataSet");
   // Read the XML document into the DataSet.
   newDataSet.ReadXml(xmlFilename);
   // 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 an XML file.
    String* xmlFilename = S"myXmlDocument.xml";
    // Use WriteXml to write the document.
    OriginalDataSet->WriteXml(xmlFilename);
    // Dispose of the original DataSet.
    OriginalDataSet->Dispose();
    // Create a new DataSet.
    DataSet* newDataSet = new DataSet(S"New DataSet");
    // Read the XML document into the DataSet.
    newDataSet->ReadXml(xmlFilename);
    // 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

.NET Framework Security: 

See Also

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

Show:
© 2014 Microsoft