System.Data Namespace DataSet Class
Reads XML schema and data into the DataSet using the specified XmlReader.
[ VB ]
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;
- reader
- The XmlReader from which to read.
The XmlReadMode used to read the data.
Use the ReadXml method to read an XML document that includes both schema and data.
To read the data from an XML document that contains only data into a DataSet, use the ReadXml method. To read just the schema from an XML document, use the ReadXmlSchema 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.
One class that inherit from the XmlReader class is the System.Xml.XmlTextReader class.
The following example first initializes 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 ReadWriteXMLDocWithXMLReader ( ) {
// 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
fsWriteXml = new System.IO.FileStream ( xmlFilename,
System.IO.FileMode.Create );
// create an XmlTextWriter to write the file.
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 ) {
Response.Write ( "<br>" + label );
foreach ( DataTable t in ds.Tables ) {
Response.Write ( "TableName: " + t.TableName );
foreach ( DataRow r in t.Rows ) {
foreach ( DataColumn c in t.Columns ) {
Response.Write ( "\t " + r [ ] );
}
Response.Write ( );
}
}
}
Private Sub ReadWriteXMLDocWithXMLReader ( )
' 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 )
Response.Write ( ControlChars.Cr + label )
Dim t As DataTable
For Each t In ds.Tables
Dim r As DataRow
Response.Write ( "TableName: " + t.TableName )
For Each r In t.Rows
Dim c As DataColumn
For Each c In t.Columns
Response.Write ( ControlChars.Tab + " " + r ( c ).ToString ( ) )
Next c
Response.Write ( )
Next r
Next t
End Sub |
|
C# |
VB |
DataSet Members DataSet.ReadXml Overload List ReadXmlSchema WriteXml WriteXmlSchema