Microsoft Knowledge Base Email Alertz

KBAlertz.com: (311570) - This article explains how to read Extensible Markup Language (XML) data into an ADO.NET DataSet object. REQUIREMENTS The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need: Microsoft...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 311570 - Last Review: May 13, 2007 - Revision: 3.2

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

This article was previously published under Q311570

On This Page

SUMMARY

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

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

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.

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  (http://kbalertz.com/Feedback.aspx?kbNumber=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.

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.

REFERENCES

For more information, click the following article number to view the article in the Microsoft Knowledge Base:
262450  (http://kbalertz.com/Feedback.aspx?kbNumber=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:
Accessing Data with ADO.NET
http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx)

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 KB311570
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please