private void WriteSchemaWithFileStream(DataTable thisDataTable)
{
// Set the file path and name. Modify this for your purposes.
string filename = "D:\\Schema.xml";
// Create the FileStream object with the file name.
// Use FileMode.Create.
System.IO.FileStream stream =
new System.IO.FileStream(filename, System.IO.FileMode.Create);
// Write the schema to the file.
thisDataTable.WriteXmlSchema(stream);
// Close the FileStream.
stream.Close();
}
private void WriteSchemaWithFileStream(DataTable thisDataTable, bool writeHierarchy)
{
// Set the file path and name. Modify this for your purposes.
string filename;
if (writeHierarchy == false)
filename = "D:\\Schema1.xml";
else
filename = "D:\\Schema2.xml";
// Create the FileStream object with the file name.
// Use FileMode.Create.
System.IO.FileStream stream =
new System.IO.FileStream(filename, System.IO.FileMode.Create);
// Write the schema to the file.
thisDataTable.WriteXmlSchema(stream, writeHierarchy);
// Close the FileStream.
stream.Close();
}
[Test]
public void TestWriteXmlSchema()
{
DataTable dt = new DataTable("TestWriteXmlSchema");
dt.Columns.Add("Col1", typeof(int));
dt.Columns.Add("Col2", typeof(int));
DataRow dr = dt.NewRow();
dr[0] = 10;
dr[1] = 20;
dt.Rows.Add (dr);
DataTable dt1 = new DataTable("HelloWorld");
dt1.Columns.Add("T1", typeof(int));
dt1.Columns.Add("T2", typeof(int));
DataRow dr1 = dt1.NewRow();
dr1[0] = 10;
dr1[1] = 20;
dt1.Rows.Add(dr1);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.Tables.Add(dt1);
DataRelation rel = new DataRelation("Relation1", dt.Columns["Col1"], dt1.Columns["T1"]);
ds.Relations.Add(rel);
WriteSchemaWithFileStream(dt);
WriteSchemaWithFileStream(dt, false);
WriteSchemaWithFileStream(dt, true);
}