How To Persist an ADO.NET DataSet into XML by Using Visual C++ .NET

Article translations Article translations
Article ID: 309184 - View products that this article applies to.
This article was previously published under Q309184
Expand all | Collapse all

On This Page

SUMMARY

This article demonstrates how to persist an ADO.NET DataSet object to Extensible Markup Language (XML).

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • A Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server, Microsoft Windows 2000 Advanced Server, or Microsoft Windows NT 4.0 Server-based computer
  • Installation of Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
  • Microsoft Visual Studio .NET
  • ADO.NET fundamentals and syntax
  • XML fundamentals

Description of the Technique

You can use the WriteXml method to write XML schema and data from the DataSet object. The XML data is written to a file, a Stream class, an XmlWriter class, or a TextWriter class. You can use one of two sets of overloaded methods for WriteXml, depending on your needs. The first set of four overloaded methods requires just one parameter, and the second set of four overloaded methods requires an additional parameter (XmlWriteMode), along with one of the previously mentioned parameters. Each of these methods is described in this section.

To write the current schema and data for the DataSet to the specified file, use the following code:
 public: void WriteXml(String*);
				
To write the current schema and data for the DataSet, use the specified TextWriter class. The TextWriter class is designed for character output.
 public: void WriteXml(TextWriter*);
				
To write the current schema and data for the DataSet, use the specified System.IO.Stream. The Stream class is designed for byte input and output.
 public: void WriteXml(Stream*);
				
To write the current schema and data for the DataSet to the specified XmlWriter, use the following code. This provides a fast, non-cached, forward-only method to generate streams or files that contain XML data to conform to the World Wide Web Consortium (W3C) XML 1.0 specification and the namespaces in the XML specification.
public: void WriteXml(XmlWriter*);
				
The XmlWriteMode enumeration specifies how to write XML data and schema from the DataSet. XmlWriteMode includes the following options:
  • DiffGram: Writes the entire DataSet as a DiffGram.
  • IgnoreSchema: Writes the current contents of the DataSet as XML data, without an XML Schema Definition (XSD) language schema.
  • WriteSchema: Writes the current contents of the DataSet as XML data, with the relational structure as inline XSD schema.

Create Project and Add Code

The following sample code creates a DataSet from the customer table in the Northwind database and uses the WriteXml method to persist the DataSet into XML. The following steps demonstrate how to use two of the commonly used overloaded versions of WriteXml. For other examples, refer to MSDN for individual overload topics related to this method.
  1. Start Visual Studio .NET.
  2. Create a new Managed C++ application project named MyApp.
  3. Copy and paste the following code to MyApp.cpp file, replacing the default code:
    
    #include "stdafx.h"
    
    #using <mscorlib.dll>
    #include <tchar.h>
    
    #using <System.dll>
    #using <System.Data.dll>
    #using <System.Xml.dll>
    
    using namespace System;
    using namespace System::Data;
    using namespace System::Data::SqlClient;
    using namespace System::Xml;
    
    void writeToXmlTextWriter();
    void writeToFile();
    // This is the entry point for this application
    int _tmain(void)
    {
        // TODO: Please replace the sample code below with your own.
        writeToXmlTextWriter();
        //comment the above line and uncomment the following line to write the xml to file. 
        //writeToFile();
    
        return 0;
    }
    void writeToXmlTextWriter()
    {
    	SqlConnection* cnn = new SqlConnection("Data Source=MySqlServer;uid=sa;pwd=sa;Initial Catalog=NorthWind");
    	SqlDataAdapter* da = new SqlDataAdapter("select * from customers",cnn);
    	DataSet* ds = new DataSet();
    
    	try
    	{
    	da->Fill(ds, "Cust");
    	}
    	catch(System::Data::SqlClient::SqlException * ex)
    	{
    		System::Console::WriteLine(ex->get_Message()->ToString());
    	}
    
    	String* strFName = "c:\\mySchema.xml";
    
    	System::IO::FileStream* myFileStream = new System::IO::FileStream(strFName, System::IO::FileMode::Create);
    	XmlTextWriter* MyXmlTextWriter = new XmlTextWriter(myFileStream, System::Text::Encoding::Unicode );
    
    	try
    	{
    	//Write the xml along with the inline schema (Default). 
    	ds->WriteXml (MyXmlTextWriter, System::Data::XmlWriteMode::WriteSchema );
    	//Write the xml only.
    	//ds->WriteXml (MyXmlTextWriter, System::Data::XmlWriteMode::IgnoreSchema  );
    	//Write the xml as a DiffGram.
    	//ds->WriteXml (MyXmlTextWriter, System::Data::XmlWriteMode::DiffGram );
    	}
    	catch(System::Exception* ex)
    	{
    		System::Console::WriteLine ("Error: {0}",ex->get_Message());
    	}
    	__finally
    	{
    		MyXmlTextWriter->Close ();
    		myFileStream->Close();
    	}
    
    	
    }
    
    void writeToFile()
    {
    	
    	SqlConnection* cnn = new SqlConnection("Data Source=MySqlServer;uid=sa;pwd=sa;Initial Catalog=NorthWind");
    	SqlDataAdapter* da = new SqlDataAdapter("select * from customers",cnn);
    	DataSet* ds = new DataSet();
    
    	da->Fill(ds, "Cust");
    
    	String* strFName = "c:\\mySchema.xml";
    
    	try
    	{
    	//Write the xml along with the inline schema (Default). 
    	ds->WriteXml (strFName, System::Data::XmlWriteMode::WriteSchema );
    	//Write the xml only.
    	//ds->WriteXml (strFName, System::Data::XmlWriteMode::IgnoreSchema  );
    	//Write the xml as a DiffGram.
    	//ds->WriteXml (strFName, System::Data::XmlWriteMode::DiffGram );
    	}
    	catch(System::Exception* ex)
    	{
    		System::Console::WriteLine ("Error: {0}",ex->get_Message());
    		
    	}
    	
    }
    					
  4. Modify the connection string (myConnectionString) and XML file path (myXMLfile) as appropriate for your environment.
  5. Save your project.
  6. On the Debug menu, click Start to run your project.
  7. Start your Web browser, and then open the XML file.

    Notice that the data in the DataSet has been persisted successfully in XML format, based on the XmlWriteMode that you specified.

Notes

  • To write only the XML schema, use the WriteXmlSchema method.
  • To get only the XML representation of the data in the Dataset, instead of persisting it onto a stream or file, use the GetXml method.

REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
262450 How To A C++ Sample of ADO Recordset XML Persistence
309702 How To Read XML Data into a DataSet by Using Visual Basic .NET
For more information about ADO.NET objects and syntax, browse to the following MSDN Web site:
Accessing Data with ADO.NET
http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx

Properties

Article ID: 309184 - Last Review: May 13, 2007 - Revision: 2.4
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
Keywords: 
kbhowtomaster kbsystemdata KB309184

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com