How to reaad XML data into DataSet by using Visual C++ .NET (311570)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework) 1.0
  • Microsoft ADO.NET (included with the .NET Framework 1.1)
  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ .NET (2003)

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

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.
  1. Start Visual Studio .NET.
  2. Create a new Managed C++ application named MyApp.
  3. 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.
    
    #include "stdafx.h"
    
    #using <mscorlib.dll>
    #include <tchar.h>
    #using <System.dll>
    using namespace System;
    
    #using <System.data.dll>
    #using <System.xml.dll>
    using namespace System::Data;
    using namespace System::Data::SqlClient;
    
    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("C:\\MySchema.xml");
    	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["Cust"]->Rows->Item[0];
    		Console::WriteLine("First row's CustomerID is: {0}", dr->Item["CustomerID"]);
    	}
    	catch(System::Exception* ex)
    	{
    		Console::WriteLine("Exception: {0}", ex->get_Message());
    	}
    	fStream->Close();
    }
    
    void readFromFile()
    {
    	String* strFName = new String("C:\\MySchema.xml");
    	DataSet* ds = new DataSet();
    	try
    	{
    		ds->ReadXml(strFName);
    		DataRow* dr = ds->Tables->Item["Cust"]->Rows->Item[0];
    		Console::WriteLine("First row's CustomerID is: {0}", dr->Item["CustomerID"]);
    	}
    	catch(System::Exception* ex)
    	{
    		Console::WriteLine("Exception: {0}", ex->get_Message());
    	}
    }
    	
  4. Modify the XML file path (MyXmlFile) as appropriate for your environment.
  5. Save your project.
  6. On the Debug menu, click Start to run your project. The CustomerID of the first row in the DataSet prints to the console window.
back to the top

Notes

  • To read only the XML schema, you can use the ReadXmlSchema method.
  • To obtain only the XML representation of the data in the Dataset (instead of persisting it onto a stream or a file), you can use the GetXml method.
back to the top

REFERENCES

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

262450 A C++ sample of ADO recordset XML persistence

For more information about ADO.NET objects and syntax, refer to the following topic in the Microsoft .NET Framework Software Development Kit (SDK) documentation: back to the top

Modification Type:MinorLast Reviewed:2/6/2006
Keywords:kbHOWTOmaster kbSystemData KB311570 kbAudDeveloper