Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Create DataSet mappings from an XSD schema?

This sample illustrates how to create DataSet mappings from a supplied XML Schema Definition language (XSD) schema. Generally, schemas are metadata, or data about data, but XSD schemas also cover the relationships between data types. From a schema, you can create a relational structure of tables and columns to store data that fits the supplied schema. This is the DataSet relational mapping of the schema.

VB DataSetMapXSDSchema.aspx
Run Sample View Source

The DataSet class has a close affiliation with the XmlDataDocument class. The DataSet class provides a relational view of the XML data loaded through the XmlDataDocument. To build a relational mapping between these two views, you need to use the ReadXmlSchema method of the DataSet. You use this method when you create the XmlDataDocument as in the following call, XmlDataDocument.DataSet.ReadXmlSchema. Now, any changes made to the DataSet are reflected in the XmlDataDocument and vice versa.

To illustrate this relationship, the following sample code reads and loads the books.xsd schema file into the DataSet property on an XmlDataDocument. Notice how the ReadXmlSchema method takes the schema loaded into a StreamReader and generates the relational mappings. If there is already a relational view defined in the XmlDataDocument, the sample throws an exception.

		
StreamReader myStreamReader = null;

try
{
    Console.WriteLine("Reading Schema file ...");
    myStreamReader = new StreamReader(schema);
    myXmlDataDocument.DataSet.ReadXmlSchema(myStreamReader);
}
catch (Exception e)
{
    Console.WriteLine ("Exception: {0}", e.ToString());
}
finally
{
    if (myStreamReader != null)
        myStreamReader.Close();
}
C#

So how do you know what the internal tables generated from the schema look like? Well, the DataSet has a Tables property that is a collection of the internal tables. Each table has a Columns collection and each column has a ColumnName and a DataType. You can simply iterate over these collections and format the output to display the internal table structure built from the supplied schema, as shown in the following code.

		
// Displays the DataSet tables structure
private void DisplayTableStructure()
{
    Console.WriteLine("\r\nTable structure \r\n");
    Console.WriteLine("Tables count=" + myXmlDataDocument.DataSet.Tables.Count.ToString());
    for (int i = 0; i < myXmlDataDocument.DataSet.Tables.Count; i++)
    {
        Console.WriteLine("\tTableName='" + myXmlDataDocument.DataSet.Tables[i].TableName + "'.");
        Console.WriteLine("\tColumns count=" + myXmlDataDocument.DataSet.Tables[i].Columns.Count.ToString());

        for (int j = 0; j < myXmlDataDocument.DataSet.Tables[i].Columns.Count; j++)
        {
            Console.WriteLine("\t\tColumnName='" +
                              myXmlDataDocument.DataSet.Tables[i].Columns[j].ColumnName + "', type = "
                              + myXmlDataDocument.DataSet.Tables[i].Columns[j].DataType.ToString());
        }
    }
}
C#

The following output shows the table names, column names, and the column types displayed by the DisplayTableStructure method for the books.xsd schema.

Table structure

Tables count=3
	TableName='bookstore'.
	Columns count=1
		ColumnName='bookstore_Id', type = System.Int32
	TableName='book'.
	Columns count=5
		ColumnName='title', type = System.String
		ColumnName='price', type = System.Decimal
		ColumnName='genre', type = System.String
		ColumnName='book_Id', type = System.Int32
		ColumnName='bookstore_Id', type = System.Int32
	TableName='author'.
	Columns count=3
		ColumnName='first-name', type = System.String
		ColumnName='last-name', type = System.String
		ColumnName='book_Id', type = System.Int32

Summary

  1. Generally, schemas are metadata, or data about data, but XSD schemas also cover the relationships between types of data.
  2. From an XSD schema, you can create a relational structure of tables and columns to store data that fits the supplied schema. This is the DataSet mapping.
  3. The ReadXmlSchema method of the DataSet generates the internal mappings from the supplied schema.



Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.