The release number of this article has been CHS311566
For Microsoft Visual Basic .NET versions of this article, see
309702.
For Microsoft Visual C .NET versions of this article, see
311570.
This article references the following Microsoft .NET Framework Class Bank Name Space:
System.data system.data.sqlclient
This task content
summary
Require technical description to create projects and add code Other Remarks
SUMMARY This article demonstrates how to read the Scalable Markup Language (XML) data into ADO.NET
DataSet objects.
Back to top
Require the following list lists the recommended hardware, software, network structure, and service pack required:
Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server or 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 Foundation and Syntax XML Foundation
Back to top
Technical note you can use
ReadXML method reads XML architecture and data into
Data set. XML data can be directly from the file,
Stream object,
XMLWRITER object or
Read in the TextWriter object.
Can be used as needed
One of the two groups of retaMLs in the two sets of overloading methods. The four overload methods in the first group take only one parameter. The four overload methods in the second group have taken one of the parameters in addition to one of the parameters in the first group.
XmlReadMode).
The following table summarizes the first group of overload methods, they take a parameter:
The following code reads an XML schema and data into a data set using the specified file: OverLoads Public Sub ReadXML (String) The following code uses the specified TextReader to read the XML schema and data into the data set. TextReader is designed for character input. Heavy Dollar Public Sub ReadXML (TextReader) The following code uses the specified System.io.Stream to read the XML schema and data into the data set. The Stream class is designed for byte input and output. Overloads Public Sub Readxml (Stream) The following code uses the specified XMLReader to read the XML schema and data into the data set. This method provides fast, cached, and only forward access. Overloads Public Sub ReadXML (XMLReader) Overview of the second group of overloaded methods, they take
XmlReadMode parameters and one of the above parameters.
XMLReadMode enumeration specifies how to read XML data and architecture into
Data set.
Diffgram. Read a DiffGram and apply changes in the DiffGram to the data set. Fragment. Read the XML document containing the Inline XML Data Simplification (XDR) architecture segment (such as those that run when the instance of Microsoft SQL Server, which is generated when the in-line XDR architecture is generated). Ignoreschema. Ignore any inline architecture and read data into an existing data set architecture. InferSchema. Ignore any inline architecture, derive the architecture from the data, and then load the data. If the dataset already contains an architecture, INFERSCHEMA expands the current architecture by adding a column and adding a new table (if the table does not exist) is added to the existing table. Readschema. Read any inline architecture and load data. Auto. default setting. Perform the most suitable operation. Back to top
Creating a project and adding code This example uses a file called myschema.xml. To create myschema.xml, follow the steps in the following Microsoft Knowledge Base article:
309183 How to: Use Visual C # .NET to save the ADO.NET dataset for XML below sample code demonstration how to use
The two most common overload versions of READXML. For additional examples, see each overload topic on this method on the MSDN.
Start Visual Studio .NET. Create a Windows application project in Visual C # .NET. The Form1 is added to the project by default. Make sure your project contains a reference to System.Data namespace, if not included, add a reference to this namespace. Put two Button controls and a DataGrid control on Form1. Change the name property of Button1 to btnReader and change its text attribute to Reader. Change the name attribute of Button2 to btnfile and then change its text attribute to File. Using the USING statement for System, System.Data, and System.Data.sqlclient namespace, you don't need to limit the declarations in these namespaces in your code. Using system;
Using system.data;
Using system.data.sqlclient; add the following code to the event handler corresponding to these buttons: Private Void BtnReader_Click (Object Sender, System.EventArgs E)
{
String myxmlfile = @ "c: /myschema.xml";
DataSet DS = New Dataset ();
// Create New FileStream with which to read the schema.
System.IO.FileStream FSReadxml = New System.IO.FileStream
(MyXMLFILE, System.IO.filemode.Open);
Try
{
DS.ReadXML (FSReadxml);
DataGrid1.datasource = DS;
DataGrid1.DataMember = "CUST";
}
Catch (Exception EX)
{
Messagebox.show (ex.totring ());
}
Finally
{
FSReadxml.close ();
}
}
Private void btnfile_click (Object Sender, System.Eventargs E)
{
String myxmlfile = "c: //myschema.xml"; DataSet DS = New Dataset ();
Try
{
DS.ReadXML (MyXMLFILE);
DataGrid1.datasource = DS;
DataGrid1.DataMember = "CUST";
}
Catch (Exception EX)
{
Messagebox.show (ex.totring ());
}
} Modify the path to the XML file according to your environment accordingly. Save the project. On the Debug menu, click Start to run your project. Click any button to read XML data from the specified file. Note that XML data will appear in the grid.
Back to top
Other memo
To read only an XML architecture, you can use the ReadXmlschema method. To get only the XML representation of the data in the data set, without keeping it to the stream or file, you can use the getXML method.
Back to top
Refer to additional information, click the following article number to view the article in the Microsoft Knowledge Base:
309183 HOW TO: Use Visual C # .NET to store the ADO.NET data set to XML
262450 HOWTO: A C Sample of Ado RecordSet XML Persistence (HOW TO: C examples of the ADO dataset for XML) For more information on ADO.NET objects and syntax, see the following Microsoft .NET Framework Software Development Kit ( SDK) Document or MSDN Online:
Use ADO.NET access data http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaccessingDatawithadonet.asp
Back to top
The information in this article applies to:
Microsoft ADO.NET (provided with .NET Frame) Microsoft Visual C # .NET (2002)
Recent Updated: 2002-6-18 (1.0) Keyword kbdsupport kbhowto kbhowtomaster kbsystemData KBXML KB311566