asp.net.ph

DataSet.ReadXml Method ( TextReader )

System.Data Namespace   DataSet Class


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

[ VB ]
Overloads Public Function ReadXml ( _
   ByVal reader As TextReader _
) As XmlReadMode

[ C# ]
public XmlReadMode ReadXml (
   TextReader reader
);

[ C++ ]
public: XmlReadMode ReadXml (
   TextReader* reader
);

[ JScript ]
public function ReadXml (
   reader : TextReader
) : XmlReadMode;

Parameters

reader
The TextReader from which to read.

Return Value

The XmlReadMode used to read the data.

Remarks

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.

Classes that inherit from the TextReader class include the StreamReader and StringReader classes.

Example

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 XML file with StreamWriter.
   string xmlFilename = "myXmlDocument.xml";
   // create a new StreamWriter.
   myStreamWriter = new System.IO.StreamWriter ( xmlFilename );
   // use WriteXml to write the document.
   OriginalDataSet.WriteXml ( myStreamWriter );
   // close the FileStream.
   myStreamWriter.Close ( );
   // dispose of the original DataSet.
   OriginalDataSet.Dispose ( );
   // create a new DataSet.
   DataSet newDataSet = new DataSet ( "New DataSet" );
   // create a StreamReader to read the file.
   myStreamReader = new System.IO.StreamReader ( xmlFilename );
   // read the XML document into the DataSet.
   newDataSet.ReadXml ( myStreamReader );
   // close the StreamReader.
   myStreamReader.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 ( );
     }
   }
}
  C# VB

See Also

DataSet Members   DataSet.ReadXml Overload List   ReadXmlSchema   WriteXml   WriteXmlSchema Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph