System.Data Namespace DataSet Class
Reads XML schema and data into the DataSet using the specified file.
[ VB ]
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;
- fileName
- The file name ( including the path ) 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.
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 ReadWriteXMLDocWithStreamReader ( ) {
// 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 ) {
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 ReadWriteXMLDocWithStreamReader ( )
' 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 )
Response.Write ( ControlChars.Cr + label )
Dim t As DataTable
For Each t In ds.Tables
Response.Write ( "TableName: " + t.TableName )
Dim r As DataRow
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