DataSet.ReadXml Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Reads XML schema and data into the DataSet.
Overloads
ReadXml(XmlReader, XmlReadMode) |
Reads XML schema and data into the DataSet using the specified XmlReader and XmlReadMode. |
ReadXml(String, XmlReadMode) |
Reads XML schema and data into the DataSet using the specified file and XmlReadMode. |
ReadXml(Stream, XmlReadMode) |
Reads XML schema and data into the DataSet using the specified Stream and XmlReadMode. |
ReadXml(TextReader, XmlReadMode) |
Reads XML schema and data into the DataSet using the specified TextReader and XmlReadMode. |
ReadXml(String) |
Reads XML schema and data into the DataSet using the specified file. |
ReadXml(TextReader) |
Reads XML schema and data into the DataSet using the specified TextReader. |
ReadXml(Stream) |
Reads XML schema and data into the DataSet using the specified Stream. |
ReadXml(XmlReader) |
Reads XML schema and data into the DataSet using the specified XmlReader. |
ReadXml(XmlReader, XmlReadMode)
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
Reads XML schema and data into the DataSet using the specified XmlReader and XmlReadMode.
public:
System::Data::XmlReadMode ReadXml(System::Xml::XmlReader ^ reader, System::Data::XmlReadMode mode);
public System.Data.XmlReadMode ReadXml (System.Xml.XmlReader? reader, System.Data.XmlReadMode mode);
public System.Data.XmlReadMode ReadXml (System.Xml.XmlReader reader, System.Data.XmlReadMode mode);
member this.ReadXml : System.Xml.XmlReader * System.Data.XmlReadMode -> System.Data.XmlReadMode
Public Function ReadXml (reader As XmlReader, mode As XmlReadMode) As XmlReadMode
Parameters
- mode
- XmlReadMode
One of the XmlReadMode values.
Returns
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 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.
Note
An InvalidOperationException will be thrown if a column type in the DataRow
being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.
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.
Note
The DataSet
does not associate an XML element with 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"
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.
See also
Applies to
ReadXml(String, XmlReadMode)
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
Reads XML schema and data into the DataSet using the specified file and XmlReadMode.
public:
System::Data::XmlReadMode ReadXml(System::String ^ fileName, System::Data::XmlReadMode mode);
public System.Data.XmlReadMode ReadXml (string fileName, System.Data.XmlReadMode mode);
member this.ReadXml : string * System.Data.XmlReadMode -> System.Data.XmlReadMode
Public Function ReadXml (fileName As String, mode As XmlReadMode) As XmlReadMode
Parameters
- fileName
- String
The filename (including the path) from which to read.
- mode
- XmlReadMode
One of the XmlReadMode values.
Returns
The XmlReadMode
used to read the data.
Exceptions
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 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.
Note
An InvalidOperationException will be thrown if a column type in the DataRow
being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.
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.
Note
The DataSet
does not associate an XML element with 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"
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.
See also
Applies to
ReadXml(Stream, XmlReadMode)
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
Reads XML schema and data into the DataSet using the specified Stream and XmlReadMode.
public:
System::Data::XmlReadMode ReadXml(System::IO::Stream ^ stream, System::Data::XmlReadMode mode);
public System.Data.XmlReadMode ReadXml (System.IO.Stream? stream, System.Data.XmlReadMode mode);
public System.Data.XmlReadMode ReadXml (System.IO.Stream stream, System.Data.XmlReadMode mode);
member this.ReadXml : System.IO.Stream * System.Data.XmlReadMode -> System.Data.XmlReadMode
Public Function ReadXml (stream As Stream, mode As XmlReadMode) As XmlReadMode
Parameters
- mode
- XmlReadMode
One of the XmlReadMode values.
Returns
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 mode
parameter, and set its value to ReadSchema
.
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.
Note
When you use ReadXml and you set XmlReadMode to Diffgram
, the content of the target DataSet
and the original DataSet
may differ because of how the diffgram is generated and processed. For more information on diffgrams, see DiffGrams.
Note
An InvalidOperationException will be thrown if a column type in the DataRow
being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.
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.
Note
The DataSet
does not associate an XML element with 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 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"
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.
See also
Applies to
ReadXml(TextReader, XmlReadMode)
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
Reads XML schema and data into the DataSet using the specified TextReader and XmlReadMode.
public:
System::Data::XmlReadMode ReadXml(System::IO::TextReader ^ reader, System::Data::XmlReadMode mode);
public System.Data.XmlReadMode ReadXml (System.IO.TextReader? reader, System.Data.XmlReadMode mode);
public System.Data.XmlReadMode ReadXml (System.IO.TextReader reader, System.Data.XmlReadMode mode);
member this.ReadXml : System.IO.TextReader * System.Data.XmlReadMode -> System.Data.XmlReadMode
Public Function ReadXml (reader As TextReader, mode As XmlReadMode) As XmlReadMode
Parameters
- reader
- TextReader
The TextReader from which to read.
- mode
- XmlReadMode
One of the XmlReadMode values.
Returns
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 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.
Note
An InvalidOperationException will be thrown if a column type in the DataRow
being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.
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.
Note
The DataSet
does not associate an XML element with 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 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"
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.
See also
Applies to
ReadXml(String)
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
Reads XML schema and data into the DataSet using the specified file.
public:
System::Data::XmlReadMode ReadXml(System::String ^ fileName);
public System.Data.XmlReadMode ReadXml (string fileName);
member this.ReadXml : string -> System.Data.XmlReadMode
Public Function ReadXml (fileName As String) As XmlReadMode
Parameters
- fileName
- String
The filename (including the path) from which to read.
Returns
The XmlReadMode
used to read the data.
Exceptions
FileIOPermission is not set to Read.
Examples
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 void DemonstrateReadWriteXMLDocumentWithStreamReader()
{
// Create a DataSet with one table and two columns.
DataSet OriginalDataSet = new DataSet("dataSet");
OriginalDataSet.Namespace= "NetFrameWork";
DataTable table = new DataTable("table");
DataColumn idColumn = new DataColumn("id",
Type.GetType("System.Int32"));
idColumn.AutoIncrement= true;
DataColumn itemColumn = new DataColumn("item");
table.Columns.Add(idColumn);
table.Columns.Add(itemColumn);
OriginalDataSet.Tables.Add(table);
// Add ten rows.
DataRow newRow;
for(int i = 0; i < 10; i++)
{
newRow = table.NewRow();
newRow["item"]= "item " + i;
table.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 = "XmlDocument.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 dataSet, string label)
{
Console.WriteLine("\n" + label);
foreach(DataTable table in dataSet.Tables)
{
Console.WriteLine("TableName: " + table.TableName);
foreach(DataRow row in table.Rows)
{
foreach(DataColumn column in table.Columns)
{
Console.Write("\table " + row[column] );
}
Console.WriteLine();
}
}
}
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", _
Type.GetType("System.Int32"))
idColumn.AutoIncrement = True
Dim itemColumn As New DataColumn("item")
table.Columns.Add(idColumn)
table.Columns.Add(itemColumn)
OriginalDataSet.Tables.Add(table)
' Add ten rows.
Dim newRow As DataRow
Dim i As Integer
For i = 0 To 9
newRow = table.NewRow()
newRow("item") = "item " & i.ToString()
table.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 = "XmlDocument.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(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 & " " & _
row(column).ToString())
Next column
Console.WriteLine()
Next row
Next table
End Sub
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 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.
Note
An InvalidOperationException will be thrown if a column type in the DataRow
being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.
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.
Note
The DataSet
does not associate an XML element with 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"
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.
See also
Applies to
ReadXml(TextReader)
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
Reads XML schema and data into the DataSet using the specified TextReader.
public:
System::Data::XmlReadMode ReadXml(System::IO::TextReader ^ reader);
public System.Data.XmlReadMode ReadXml (System.IO.TextReader? reader);
public System.Data.XmlReadMode ReadXml (System.IO.TextReader reader);
member this.ReadXml : System.IO.TextReader -> System.Data.XmlReadMode
Public Function ReadXml (reader As TextReader) As XmlReadMode
Parameters
- reader
- TextReader
The TextReader
from which to read the schema and data.
Returns
The XmlReadMode used to read the data.
Examples
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 void DemonstrateReadWriteXMLDocumentWithFileStream()
{
// Create a DataSet with one table and two columns.
DataSet originalDataSet = new DataSet("dataSet");
DataTable table = new DataTable("table");
DataColumn idColumn = new DataColumn("id",
Type.GetType("System.Int32"));
idColumn.AutoIncrement= true;
DataColumn itemColumn = new DataColumn("item");
table.Columns.Add(idColumn);
table.Columns.Add(itemColumn);
originalDataSet.Tables.Add(table);
// Add ten rows.
DataRow newRow;
for(int i = 0; i < 10; i++)
{
newRow = table.NewRow();
newRow["item"]= "item " + i;
table.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 FileStream.
string xmlFilename = "XmlDocument.xml";
System.IO.FileStream streamWrite = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Create);
// Use WriteXml to write the XML document.
originalDataSet.WriteXml(streamWrite);
// Close the FileStream.
streamWrite.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 streamRead = new System.IO.FileStream
(xmlFilename,System.IO.FileMode.Open);
newDataSet.ReadXml(streamRead);
// Print out values of each table in the DataSet
// using the function defined below.
PrintValues(newDataSet,"New DataSet");
}
private void PrintValues(DataSet dataSet, string label)
{
Console.WriteLine("\n" + label);
foreach(DataTable table in dataSet.Tables)
{
Console.WriteLine("TableName: " + table.TableName);
foreach(DataRow row in table.Rows)
{
foreach(DataColumn column in table.Columns)
{
Console.Write("\table " + row[column] );
}
Console.WriteLine();
}
}
}
Private Sub DemonstrateReadWriteXMLDocumentWithFileStream()
' Create a DataSet with one table and two columns.
Dim originalDataSet As New DataSet("dataSet")
Dim table As New DataTable("table")
Dim idColumn As New DataColumn("id", _
Type.GetType("System.Int32"))
idColumn.AutoIncrement = True
Dim itemColumn As New DataColumn("item")
table.Columns.Add(idColumn)
table.Columns.Add(itemColumn)
originalDataSet.Tables.Add(table)
' Add ten rows.
Dim newRow As DataRow
Dim i As Integer
For i = 0 To 9
newRow = table.NewRow()
newRow("item") = "item " & i.ToString()
table.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 FileStream.
Dim xmlFilename As String = "XmlDocument.xml"
Dim streamWrite As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Create)
' Use WriteXml to write the XML document.
originalDataSet.WriteXml(streamWrite)
' Close the FileStream.
streamWrite.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 streamRead As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Open)
newDataSet.ReadXml(streamRead)
' 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
Dim row As DataRow
Dim column As DataColumn
For Each table In dataSet.Tables
Console.WriteLine("TableName: " & table.TableName)
For Each row In table.Rows
For Each column In table.Columns
Console.Write(ControlChars.Tab & " " & _
row(column).ToString())
Next column
Console.WriteLine()
Next row
Next table
End Sub
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 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.
Note
An InvalidOperationException will be thrown if a column type in the DataRow
being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.
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.
Note
The DataSet
does not associate an XML element with 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 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"
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>
Classes that inherit from the TextReader class include the StreamReader and StringReader classes.
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.
See also
Applies to
ReadXml(Stream)
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
public:
System::Data::XmlReadMode ReadXml(System::IO::Stream ^ stream);
public System.Data.XmlReadMode ReadXml (System.IO.Stream? stream);
public System.Data.XmlReadMode ReadXml (System.IO.Stream stream);
member this.ReadXml : System.IO.Stream -> System.Data.XmlReadMode
Public Function ReadXml (stream As Stream) As XmlReadMode
Parameters
Returns
The XmlReadMode used to read the data.
Examples
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 void DemonstrateReadWriteXMLDocumentWithFileStream()
{
// Create a DataSet with one table and two columns.
DataSet originalDataSet = new DataSet("dataSet");
DataTable table = new DataTable("table");
DataColumn idColumn = new DataColumn("id",
Type.GetType("System.Int32"));
idColumn.AutoIncrement= true;
DataColumn itemColumn = new DataColumn("item");
table.Columns.Add(idColumn);
table.Columns.Add(itemColumn);
originalDataSet.Tables.Add(table);
// Add ten rows.
DataRow newRow;
for(int i = 0; i < 10; i++)
{
newRow = table.NewRow();
newRow["item"]= "item " + i;
table.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 FileStream.
string xmlFilename = "XmlDocument.xml";
System.IO.FileStream streamWrite = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Create);
// Use WriteXml to write the XML document.
originalDataSet.WriteXml(streamWrite);
// Close the FileStream.
streamWrite.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 streamRead = new System.IO.FileStream
(xmlFilename,System.IO.FileMode.Open);
newDataSet.ReadXml(streamRead);
// Print out values of each table in the DataSet
// using the function defined below.
PrintValues(newDataSet,"New DataSet");
}
private void PrintValues(DataSet dataSet, string label)
{
Console.WriteLine("\n" + label);
foreach(DataTable table in dataSet.Tables)
{
Console.WriteLine("TableName: " + table.TableName);
foreach(DataRow row in table.Rows)
{
foreach(DataColumn column in table.Columns)
{
Console.Write("\table " + row[column] );
}
Console.WriteLine();
}
}
}
Private Sub DemonstrateReadWriteXMLDocumentWithFileStream()
' Create a DataSet with one table and two columns.
Dim originalDataSet As New DataSet("dataSet")
Dim table As New DataTable("table")
Dim idColumn As New DataColumn("id", _
Type.GetType("System.Int32"))
idColumn.AutoIncrement = True
Dim itemColumn As New DataColumn("item")
table.Columns.Add(idColumn)
table.Columns.Add(itemColumn)
originalDataSet.Tables.Add(table)
' Add ten rows.
Dim newRow As DataRow
Dim i As Integer
For i = 0 To 9
newRow = table.NewRow()
newRow("item") = "item " & i.ToString()
table.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 FileStream.
Dim xmlFilename As String = "XmlDocument.xml"
Dim streamWrite As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Create)
' Use WriteXml to write the XML document.
originalDataSet.WriteXml(streamWrite)
' Close the FileStream.
streamWrite.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 streamRead As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Open)
newDataSet.ReadXml(streamRead)
' 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
Dim row As DataRow
Dim column As DataColumn
For Each table In dataSet.Tables
Console.WriteLine("TableName: " & table.TableName)
For Each row In table.Rows
For Each column In table.Columns
Console.Write(ControlChars.Tab & " " & _
row(column).ToString())
Next column
Console.WriteLine()
Next row
Next table
End Sub
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 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.
Note
An InvalidOperationException will be thrown if a column type in the DataRow
being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.
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.
Note
The DataSet
does not associate an XML element with 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 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"
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.
See also
Applies to
ReadXml(XmlReader)
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
public:
System::Data::XmlReadMode ReadXml(System::Xml::XmlReader ^ reader);
public System.Data.XmlReadMode ReadXml (System.Xml.XmlReader? reader);
public System.Data.XmlReadMode ReadXml (System.Xml.XmlReader reader);
member this.ReadXml : System.Xml.XmlReader -> System.Data.XmlReadMode
Public Function ReadXml (reader As XmlReader) As XmlReadMode
Parameters
Returns
The XmlReadMode
used to read the data.
Examples
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 void DemonstrateReadWriteXMLDocumentWithXMLReader()
{
// Create a DataSet with one table and two columns.
DataSet OriginalDataSet = new DataSet("dataSet");
OriginalDataSet.Namespace= "NetFrameWork";
DataTable table = new DataTable("table");
DataColumn idColumn = new DataColumn("id",
Type.GetType("System.Int32"));
idColumn.AutoIncrement= true;
DataColumn itemColumn = new DataColumn("item");
table.Columns.Add(idColumn);
table.Columns.Add(itemColumn);
OriginalDataSet.Tables.Add(table);
// Add ten rows.
DataRow newRow;
for(int i = 0; i < 10; i++)
{
newRow = table.NewRow();
newRow["item"]= "item " + i;
table.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 = "XmlDocument.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, XmlWriteMode.WriteSchema);
// 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 xmlReader =
new System.Xml.XmlTextReader(fsReadXml);
// Read the XML document into the DataSet.
newDataSet.ReadXml(xmlReader, XmlReadMode.ReadSchema);
// Close the XmlTextReader
xmlReader.Close();
// Print out values of each table in the DataSet
// fusing the unction defined below.
PrintValues(newDataSet,"New DataSet");
}
private void PrintValues(DataSet dataSet, string label)
{
Console.WriteLine("\n" + label);
foreach(DataTable table in dataSet.Tables)
{
Console.WriteLine("TableName: " + table.TableName);
foreach(DataRow row in table.Rows)
{
foreach(DataColumn column in table.Columns)
{
Console.Write("\t{0}({1})='{2}'", column.ColumnName, column.DataType.Name, row[column]);
}
Console.WriteLine();
}
}
}
Private Sub DemonstrateReadWriteXMLDocumentWithXMLReader()
' 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", _
Type.GetType("System.Int32"))
idColumn.AutoIncrement = True
Dim itemColumn As New DataColumn("item")
table.Columns.Add(idColumn)
table.Columns.Add(itemColumn)
OriginalDataSet.Tables.Add(table)
' Add ten rows.
Dim newRow As DataRow
Dim i As Integer
For i = 0 To 9
newRow = table.NewRow()
newRow("item") = "item " & i.ToString()
table.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 = "XmlDocument.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, XmlWriteMode.WriteSchema)
' 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 xmlReader As New System.Xml.XmlTextReader(fsReadXml)
' Read the XML document into the DataSet.
newDataSet.ReadXml(xmlReader, XmlReadMode.ReadSchema)
' Close the XmlTextReader
xmlReader.Close()
' 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
Dim row As DataRow
Dim column As DataColumn
For Each table In dataSet.Tables
Console.WriteLine("TableName: " & table.TableName)
For Each row In table.Rows
For Each column In table.Columns
Console.Write(ControlChars.Tab & "{0}({1})='{2}'", column.ColumnName, column.DataType.Name, row(column).ToString())
Next column
Console.WriteLine()
Next row
Next table
End Sub
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 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.
Note
An InvalidOperationException will be thrown if a column type in the DataRow
being read from or written to implements IDynamicMetaObjectProvider and does not implement IXmlSerializable.
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.
Note
The DataSet
does not associate an XML element with 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"
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.