Partager via


Loading DataSet Schema Information from XML

The schema of a DataSet (its tables, columns, relations, and constraints) can be defined programmatically, created by the Fill or FillSchema methods of a DataAdapter, or loaded from an XML document. To load DataSet schema information from an XML document, you can use either the ReadXmlSchema or the InferXmlSchema method of the DataSet. ReadXmlSchema allows you to load or infer DataSet schema information from the document containing XML Schema definition language (XSD) schema, or an XML document with inline XML Schema. InferXmlSchema allows you to infer the schema from the XML document while ignoring certain XML namespaces that you specify.

ReadXmlSchema

To load the schema of a DataSet from an XML document, without loading any data, you can use the ReadXmlSchema method of the DataSet. ReadXmlSchema creates DataSet schema defined using XML Schema definition language (XSD) schema.

The ReadXmlSchema method takes a single argument of a file name, a stream, or an XmlReader containing the XML document to be loaded. The XML document can contain only schema, or can contain schema inline with XML elements containing data. For details about writing inline schema as XML Schema, see Generating DataSet Relational Structure from XML Schema (XSD).

If the XML document passed to ReadXmlSchema contains no inline schema information, ReadXmlSchema will infer the schema from the elements in the XML document. If the DataSet already contains a schema, the current schema will be extended by adding new columns to existing tables and adding new tables if they do not already exist. If a column being added already exists in the DataSet but has an incompatible type with the column found in the XML, an exception will be thrown. For details about how ReadXmlSchema infers a schema from an XML document, see Inferring DataSet Relational Structure from XML.

While ReadXmlSchema loads or infers only the schema of a DataSet, the ReadXml method of the DataSet will load or infer both the schema and the data contained in the XML document. For more information, see Loading a DataSet from XML.

The following code examples show how to load a DataSet schema from an XML document or stream. The first example shows an XML Schema file name being passed to the ReadXmlSchema method. The second example shows a System.IO.StreamReader.

Dim myDS As DataSet = New DataSet
myDS.ReadXmlSchema("schema.xsd")
[C#]
DataSet myDS = new DataSet();
myDS.ReadXmlSchema("schema.xsd");
[Visual Basic]
Dim xmlStream As System.IO.StreamReader = New System.IO.StreamReader ("schema.xsd");
Dim myDS As DataSet = New DataSet
myDS.ReadXmlSchema(xmlStream)
xmlStream.Close()
[C#]
System.IO.StreamReader xmlStream = new System.IO.StreamReader("schema.xsd");
DataSet myDS = new DataSet();
myDS.ReadXmlSchema(xmlStream);
xmlStream.Close();

InferXmlSchema

You can also instruct the DataSet to infer its schema from an XML document using the InferXmlSchema method of the DataSet. InferXmlSchema functions the same as do both ReadXml with an XmlReadMode of InferSchema (loads data as well as infers schema), and ReadXmlSchema if the document being read contains no inline schema. However, InferXmlSchema provides the additional capability of allowing you to specify particular XML namespaces to be ignored when the schema is inferred. InferXmlSchema takes two required arguments: the location of the XML document, specified by a file name, a stream, or an XmlReader; and a string array of XML namespaces to be ignored by the operation.

For example, consider the following XML:

<NewDataSet xmlns:od="urn:schemas-microsoft-com:officedata">
<Categories>
  <CategoryID od:adotype="3">1</CategoryID> 
  <CategoryName od:maxLength="15" od:adotype="130">Beverages</CategoryName> 
  <Description od:adotype="203">Soft drinks and teas</Description> 
</Categories>
<Products>
  <ProductID od:adotype="20">1</ProductID> 
  <ReorderLevel od:adotype="3">10</ReorderLevel> 
  <Discontinued od:adotype="11">0</Discontinued> 
</Products>
</NewDataSet>

Because of the attributes specified for the elements in the preceding XML document, the ReadXmlSchema method, as well as the ReadXml method with an XmlReadMode of InferSchema, would both create tables for every element in the document: Categories, CategoryID, CategoryName, Description, Products, ProductID, ReorderLevel, and Discontinued. (For more information, see Inferring DataSet Relational Structure from XML.) However, a more appropriate structure would be to create only the Categories and Products tables, and then to create CategoryID, CategoryName, and Description columns in the Categories table, and ProductID, ReorderLevel, and Discontinued columns in the Products table. To ensure that the inferred schema ignores the attributes specified in the XML elements, use the InferXmlSchema method and specify the XML namespace for officedata to be ignored, as shown in the following example.

Dim myDS As DataSet = New DataSet
myDS.InferXmlSchema("input_od.xml", New String[] {"urn:schemas-microsoft-com:officedata"})
[C#]
DataSet myDS = new DataSet();
myDS.InferXmlSchema("input_od.xml", new string[] "urn:schemas-microsoft-com:officedata");

See Also

XML and the DataSet | Generating DataSet Relational Structure from XML Schema (XSD) | Inferring DataSet Relational Structure from XML | Loading a DataSet from XML | Creating and Using DataSets