Writing DataSet Schema Information as XML Schema (XSD)

You can write the schema of a DataSet (its tables, columns, relations, and constraints) as XML Schema definition language (XSD) schema, so that you can transport it, with or without related data, in an XML document. XML Schema, which can be written to a file, a stream, an XmlWriter, or a string, is useful for generating a strongly typed DataSet. For more information about strongly typed DataSet objects, see Working with a Typed DataSet.

You can specify how a column of a table is represented in XML Schema using the ColumnMapping property of the DataColumn object. For more information, see the note on "Mapping Columns to XML Elements, Attributes, and Text" in Writing a DataSet as XML Data.

To write the schema of a DataSet as XML Schema, to a file, stream, or XmlWriter, use the WriteXmlSchema method of the DataSet. WriteXmlSchema takes one parameter that specifies the destination of the resulting XML Schema. The following code examples demonstrate how to write the XML Schema of a DataSet to a file by passing a string containing a file name and a System.IO.StreamWriter object.

custDS.WriteXmlSchema("Customers.xsd")
[C#]
custDS.WriteXmlSchema("Customers.xsd");
[Visual Basic]
Dim xmlSW As System.IO.StreamWriter = New System.IO.StreamWriter("Customers.xsd")
custDS.WriteXmlSchema(xmlSW)
xmlSW.Close()
[C#]
System.IO.StreamWriter xmlSW = new System.IO.StreamWriter("Customers.xsd");
custDS.WriteXmlSchema(xmlSW);
xmlSW.Close();

To obtain the schema of a DataSet and write it as an XML Schema string, use the GetXmlSchema method as shown in the following example.

Dim xsdDS As String = custDS.GetXmlSchema()
[C#]
string xsdDS = custDS.GetXmlSchema();

See Also

XML and the DataSet | Writing a DataSet as XML Data | Working with a Typed DataSet | Creating and Using DataSets