How Do I...Save DataSet mappings to an XSD schema file?
This sample illustrates how to save the internal DataSet mappings to an XML Schema Definition language (XSD) schema file. This sample builds the mappings by using the relational methods on the DataSet to create tables and columns. The sample then writes an XSD schema representation of these mappings out to a file.
VB SaveDataSetMapXSDSchema.aspx
The DataSet and XmlDataDocument classes both represent an in-memory data cache. The DataSet provides relationally orientated navigational and editing methods, while the XmlDataDocument provides XML navigational and editing methods.
This sample gets the DataSet property from the XmlDataDocument, uses it to build a set of tables and columns, and then populate those tables and columns. The sample then writes out the internally generated schema.
The following sample code builds two tables, one of people and the other of pets. The sample uses an ID as a primary key into each of the tables, and builds a relationship table between the people and their pets. To do this, the sample first creates an instance of an XmlDataDocument and then passes the DataSet associated with the XmlDataDocument to the LoadDataSet method.
XmlDataDocument datadoc = new XmlDataDocument();
LoadDataSet(datadoc.DataSet);
C#
The LoadDataSet method loads the dataset with relational data.
// Load a DataSet with relational data
private void LoadDataSet(DataSet dataset)
{
try
{
Console.WriteLine("Loading the DataSet ...");
// Set DataSet name
dataset.DataSetName = "PersonPet";
// Create tables for people and pets
DataTable people = new DataTable("Person");
DataTable pets = new DataTable("Pet");
// Set up the columns in the Tables
DataColumn personname = new DataColumn ("Name", typeof(String));
DataColumn personAge = new DataColumn ("Age", typeof(Int32));
DataColumn petname = new DataColumn ("Name", typeof(String));
DataColumn pettype = new DataColumn ("Type", typeof(String));
// Add columns to person table
DataColumn id = people.Columns.Add("ID", typeof(Int32));
id.AutoIncrement = true;
people.PrimaryKey = new DataColumn[] {id};
people.Columns.Add (personname);
people.Columns.Add (personAge);
// Add columns to pet table
id = pets.Columns.Add("ID", typeof(Int32));
id.AutoIncrement = true;
pets.PrimaryKey = new DataColumn[] {id};
id.AutoIncrement = true;
DataColumn ownerid = pets.Columns.Add("OwnerID", typeof(Int32));
DataColumn[] foreignkey = new DataColumn[] {ownerid};
pets.Columns.Add (petname);
pets.Columns.Add (pettype);
// Add tables to the DataSet
dataset.Tables.Add (people);
dataset.Tables.Add (pets);
// Add people
DataRow mark = people.NewRow();
mark[personname] = "Mark";
mark[personAge] = 18;
people.Rows.Add(mark);
DataRow william = people.NewRow();
william[personname] = "William";
william[personAge] = 12;
people.Rows.Add(william);
DataRow james = people.NewRow();
james[personname] = "James";
james[personAge] = 7;
people.Rows.Add(james);
DataRow levi = people.NewRow();
levi[personname] = "Levi";
levi[personAge] = 4;
people.Rows.Add(levi);
// Add relationships
Console.WriteLine("Creating relationships between people and pets ...");
DataRelation personpetrel = new DataRelation ("PersonPet",people.PrimaryKey, foreignkey);
dataset.Relations.Add (personpetrel);
// Add pets
DataRow row = pets.NewRow();
row["OwnerID"] = mark["ID"];
row[petname] = "Frank";
row[pettype] = "cat";
pets.Rows.Add(row);
row = pets.NewRow();
row["OwnerID"] = william["ID"];
row[petname] = "Rex";
row[pettype] = "dog";
pets.Rows.Add(row);
row = pets.NewRow();
row["OwnerID"] = james["ID"];
row[petname] = "Cottontail";
row[pettype] = "rabbit";
pets.Rows.Add(row);
row = pets.NewRow();
row["OwnerID"] = levi["ID"];
row[petname] = "Sid";
row[pettype] = "snake";
pets.Rows.Add(row);
row = pets.NewRow();
row["OwnerID"] = levi["ID"];
row[petname] = "Tickles";
row[pettype] = "spider";
pets.Rows.Add(row);
row = pets.NewRow();
row["OwnerID"] = william["ID"];
row[petname] = "Tweetie";
row[pettype] = "canary";
pets.Rows.Add(row);
// commit changes
dataset.AcceptChanges();
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.ToString());
}
}
C#
The AcceptChanges method of the DataSet commits all the changes that have been made to this DataSet since it was loaded or the last time AcceptChanges was called. All new and modified rows become unchanged, and deleted rows get removed. For more detail on the other DataSet relational methods, see How do I...Get an Overview of ADO.NET?
To save the schema to a file, the sample calls the WriteXmlSchema method of the DataSet, passing a StreamWriter class that represents the destination file.
StreamWriter writer = null;
String mySaveSchema = Environment.GetEnvironmentVariable("TEMP") +
"\\PersonPetCS.xsd";
try
{
Console.WriteLine("Writing the schema to {0} ...", mySaveSchema);
writer = new StreamWriter(mySaveSchema);
datadoc.DataSet.WriteXmlSchema(writer);
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.ToString());
}
finally
{
if (writer != null)
writer.Close();
}
C#
The following output shows the tables built in the DataSet by the DisplayTables method. For more information about the DisplayTables method, see How do I...Infer DataSet Mappings from XML?. The sample writes the inferred schema to the PersonPet.xsd file.
Loading the DataSet ...
Creating relationships between people and pets ...
DataSet:
PersonPet contains ...
No of Tables: 2 Table content ...
TableName = Person
---------
Columns ...
ID Name Age
Number of rows = 4
Rows ...
0 Mark 18
1 William 12
2 James 19
3 Levi 4
TableName = Pet
---------
Columns ...
ID OwnerID Name Type
Number of rows = 6
Rows ...
0 0 Frank cat
1 1 Rex dog
2 2 Cottontail rabbit
3 3 Sid snake
4 3 Tickles spider
5 1 Tweetie canary
PersonPet
Name = Mark owns
Pet = Frank the cat
Name = William owns
Pet = Rex the dog
Pet = Tweetie the canary
Name = James owns
Pet = Cottontail the rabbit
Name = Levi owns
Pet = Sid the snake
Pet = Tickles the spider
Writing the schema to ...\LOCALS~1\Temp\PersonPet.xsd ...
Summary
- The WriteXmlSchema method saves the mappings for the internal structure of the relation data in the DataSet as an XSD Schema.
- The XmlDataDocument has a DataSet property that enables you to view and manage structured data relationally within an XML document.
Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2004 Microsoft Corporation. All rights reserved.
|