Microsoft KB Archive/311570

= How to reaad XML data into DataSet by using Visual C++ .NET =

Article ID: 311570

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.0
 * Microsoft ADO.NET 1.1
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-



This article was previously published under Q311570





For a Microsoft Visual Basic .NET version of this article, see 309702.



For a Microsoft C# .NET version of this article, see 311566.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System::Data
 * System::Data::SqlClient

IN THIS TASK
SUMMARY
 * Requirements
 * Description of the Technique
 * Create Project and Add Code
 * Notes

REFERENCES



SUMMARY
This article explains how to read Extensible Markup Language (XML) data into an ADO.NET DataSet object.

back to the top

REQUIREMENTS
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server, or Microsoft Windows NT 4.0 Server
 * Microsoft Visual Studio .NET

This article assumes that you are familiar with the following topics:
 * Visual Studio .NET
 * ADO.NET fundamentals and syntax
 * XML fundamentals

back to the top

Description of the Technique
You can use the ReadXml method to read XML schema and data into a DataSet. The XML data can be read directly from a file, or by using a Stream class, an XmlWriter class, or a TextWriter object. There are two sets of overloaded methods for the ReadXml method; the one you choose depends on your need.

The first set of four overloaded methods takes just one parameter. The second set of four overloaded methods takes an additional parameter (XmlReadMode) along with one of the parameters specified in this section.

The code to follow uses a specified file to read XML schema and data into the DataSet: public: XmlReadMode ReadXml(String*); The code to follow uses a specified TextReader to read XML schema and data into the DataSet. TextReader is designed for character input. public: XmlReadMode ReadXml(TextReader*); The code to follow uses a specified System.IO.Stream to read XML schema and data into the DataSet. The Stream class is designed for byte input and output. public: XmlReadMode ReadXml(Stream*); The code to follow uses a specified XmlReader to read XML schema and data into the DataSet. This provides fast, non-cached, forward-only access to XML data that conforms to the World Wide Web Consortium (W3C) XML 1.0 specification and the namespaces in the XML specification. public: XmlReadMode ReadXml(XmlReader*); The list to follow outlines the second set of overloaded methods, which take XmlReadMode with one of the parameters listed earlier. The XmlReadMode enumeration specifies how to read XML data and schema into a DataSet. XmlReadMode includes the following options:
 * DiffGram. Reads a DiffGram, applying changes from the DiffGram to the DataSet.
 * Fragment. Reads XML documents containing inline XML-Data Reduced (XDR) schema fragments, such as those that are generated when you run FOR XML schemas that include inline XDR schema against an instance of SQL Server.
 * IgnoreSchema. Ignores any inline schema and reads data into the existing DataSet schema.
 * InferSchema. Ignores any inline schema, infers schema from the data, and loads the data. If the DataSet already contains a schema, InferSchema extends the current schema by adding columns to existing tables and by adding new tables where necessary.
 * ReadSchema. Reads any inline schema and loads the data.
 * Auto (default). Performs the most appropriate action.

back to the top

Create Project and Add Code
This example uses a file named MySchema.xml. To create MySchema.xml, follow the steps in the following Microsoft Knowledge Base article:

309184 How to persist an ADO.NET DataSet into XML by using Visual C++ .NET

The sample shows how to use two of the commonly used overloaded versions of ReadXml.

For other examples and benefits, refer to MSDN for individual overload topics of this method.  Start Visual Studio .NET. Create a new Managed C++ application named MyApp.  Replace the default code in MyApp.cpp with the following code: // This is the main project file for VC++ application // that the Application Wizard generates.


 * 1) include &quot;stdafx.h&quot;

using namespace System;
 * 1) using 
 * 2) include 
 * 3) using 

using namespace System::Data; using namespace System::Data::SqlClient;
 * 1) using 
 * 2) using 

void readFromStream; void readFromFile; // This is the entry point for this application. int _tmain(void) {   // TODO: Replace the sample code below with your own code. readFromStream; // Comment the above line, and uncomment the next line to read the XML data from a file. // readFromFile;

return 0; } void readFromStream {   String* strFName = new String(&quot;C:\\MySchema.xml&quot;); DataSet* ds = new DataSet; System::IO::FileStream * fStream = new System::IO::FileStream(strFName, System::IO::FileMode::Open ); try {

ds->ReadXml(fStream); DataRow* dr = ds->Tables->Item[&quot;Cust&quot;]->Rows->Item[0]; Console::WriteLine(&quot;First row's CustomerID is: {0}&quot;, dr->Item[&quot;CustomerID&quot;]); }   catch(System::Exception* ex) {       Console::WriteLine(&quot;Exception: {0}&quot;, ex->get_Message); }   fStream->Close; }

void readFromFile {   String* strFName = new String(&quot;C:\\MySchema.xml&quot;); DataSet* ds = new DataSet; try {       ds->ReadXml(strFName); DataRow* dr = ds->Tables->Item[&quot;Cust&quot;]->Rows->Item[0]; Console::WriteLine(&quot;First row's CustomerID is: {0}&quot;, dr->Item[&quot;CustomerID&quot;]); }   catch(System::Exception* ex) {       Console::WriteLine(&quot;Exception: {0}&quot;, ex->get_Message); } }   </li> Modify the XML file path (MyXmlFile) as appropriate for your environment.</li> Save your project.</li> On the Debug menu, click Start to run your project. The CustomerID of the first row in the DataSet prints to the console window.</li></ol>

back to the top