How Do I...Infer DataSet mappings from XML?
This sample illustrates how to create DataSet mappings from XML Data. This sample uses the concept of inference, whereby the actual structure of the XML data itself is the basis for a relational schema. The sample then creates a relational structure of tables and columns to store data in a DataSet that conforms to the inferred schema.
VB DataSetMapXMLData.aspx
In order to infer a schema from the XML data structure, the following sample code demonstrates how you should call DataSet.ReadXml using InferSchema for the XmlReadMode. If you do not specify an XmlReadMode, the ReadXml method infers a schema if both the dataset does not already have a schema and the document does not contain an inline schema.
// Infer the DataSet schema from the XML data and load the XML Data
datadoc.DataSet.ReadXml(new StreamReader("books.xml"), XmlReadMode.InferSchema);
C#
As in How Do I...Load a DataSet with XML?, this sample also uses the DisplayTables method to display the structure and contents of the DataSet. The following output shows the table names, column names, and row contents generated when the DisplayTables method uses the schema inferred from the
books.xml file.
Creating an XmlDataDocument ...
Content of Tables ...
TableName = book
---------
Columns ...
title book_Id price genre publicationdate ISBN
Number of rows = 3
Rows ...
The Autobiography of Benjamin Franklin 0 8.99 autobiography 1981 1-861003-11-0
The Confidence Man 1 11.99 novel 1967 0-201-63361-2
The Gorgias 2 9.99 philosophy 1991 1-861001-57-6
TableName = author
---------
Columns ...
first-name last-name book_Id
Number of rows = 3
Rows ...
Benjamin Franklin 0
Herman Melville 1
Sidas Plato 2
Summary
- Inference is the mechanism whereby the structure of the XML data is the basis for a relational schema.
- The ReadXml method of the DataSet builds relational mapping using the supplied XML file in a StreamReader.
Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.
|