<h4>How Do I...Save DataSet mappings to an XSD schema file?</h4>
<div class="indent" style="width:660">
This sample illustrates how to save the internal DataSet mappings to an XSD schema file. In this sample the mappings are built by using the relational methods
on the DataSet to create tables and columns and an XSD schema representation for these is then written out to a file.</div>
<h4>Saving DataSet mappings to an XSD schema file</h4>
The DataSet and XmlDataDocument classes both represent an in-memory data cache. The XmlDataDocument provides XML navigational and editing methods whilst the DataSet
provides relationally orientated navigational and editing methods. Here we are going to get the DataSet property from the XmlDataDocument, use it to build a set of
tables and columns and then populate them. We can then write out the internally generated schema. The code illustrated below builds up two tables, one of people
and the other of pets. An ID is used as a primary key onto the each of the tables and a relationship table is built between the people and their pets.
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: ", e.ToString());
}
}
</xmp></div>
<div class="indent" style="width:660">
The DataSet <b>AcceptChanges</b> method 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
<A target=content href="/quickstart/howto/doc/adoplus/adoplusoverview.aspx">How do I...Get an Overview of ADO+?</A></div>