asp.net.ph

DataSet.ReadXml Method ( XmlReader )

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;

Parameters

reader
The XmlReader 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.

One class that inherit from the XmlReader class is the System.Xml.XmlTextReader class.

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 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 ( );
     }
   }
}
  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