This documentation is archived and is not being maintained.

DataSet.ReadXml Method (String)

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

Namespace: System.Data
Assembly: System.Data (in

Public Function ReadXml ( _
	fileName As String _
) As XmlReadMode
Dim instance As DataSet
Dim fileName As String
Dim returnValue As XmlReadMode

returnValue = instance.ReadXml(fileName)
public XmlReadMode ReadXml (
	String fileName
public function ReadXml (
	fileName : String
) : XmlReadMode
Not applicable.



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

Return Value

The XmlReadMode used to read the data.

Exception typeCondition


FileIOPermission is not set to Read.

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 mode 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 data types) 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.


The DataSet does not associate an XML elementwith its corresponding DataColumn or DataTable when legal XML characters like ("_") are escaped in the serialized XML. The DataSet itself only escapes illegal XML characters in XML element names and hence can only consume the same. When legal characters in XML element name are escaped, the element is ignored while processing.

If the XML 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 XML Schema as the following example demonstrates.

 <xsd:schema id="MyDataSet" 

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.

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.

Private Sub DemonstrateReadWriteXMLDocumentWithStreamReader()
    ' Create a DataSet with one table and two columns.
    Dim OriginalDataSet As New DataSet("dataSet")
    OriginalDataSet.Namespace = "NetFrameWork"
    Dim table As New DataTable("table")
    Dim idColumn As New DataColumn("id", _
    idColumn.AutoIncrement = True

    Dim itemColumn As New DataColumn("item")

    ' Add ten rows.
    Dim newRow As DataRow
    Dim i As Integer
    For i = 0 To 9
        newRow = table.NewRow()
        newRow("item") = "item " & i.ToString()
    Next i

    ' 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 = "XmlDocument.xml"

    ' Use WriteXml to write the document.

    ' Dispose of the original DataSet.

    ' Create a new DataSet.
    Dim newDataSet As New DataSet("New DataSet")

    ' Read the XML document into the DataSet.

    ' Print out values of each table in the DataSet 
    ' using the function defined below.
    PrintValues(newDataSet, "New DataSet")
End Sub
Private Sub PrintValues(dataSet As DataSet, label As String)
    Console.WriteLine(ControlChars.Cr & label)
    Dim table As DataTable
    For Each table In  dataSet.Tables
        Console.WriteLine("TableName: " & table.TableName)
        Dim row As DataRow
        For Each row In  table.Rows
            Dim column As DataColumn
            For Each column In  table.Columns
                Console.Write(ControlChars.Tab & " " & _
            Next column
        Next row
    Next table
End Sub

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0