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.

Note

Table ordering in a DataSet might not be preserved when you use Web services or XML serialization to transfer a DataSet that was created in-memory by using XSD constructs (such as nested relations). Therefore, the recipient of the DataSet should not depend on table ordering in this case. However, table ordering is always preserved if the schema of the DataSet being transferred was read from XSD files, instead of being created in-memory.

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 Deriving 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 tables if they do not already exist. New columns will not be added to added to existing tables. If a column being added already exists in the DataSet but has an incompatible type with the column found in the XML, an exception is thrown. For details about how ReadXmlSchema infers a schema from an XML document, see Inferring DataSet Relational Structure from XML.

Although ReadXmlSchema loads or infers only the schema of a DataSet, the ReadXml method of the DataSet loads or infers 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 dataSet As DataSet = New DataSet  
dataSet.ReadXmlSchema("schema.xsd")  
DataSet dataSet = new DataSet();  
dataSet.ReadXmlSchema("schema.xsd");  
Dim xmlStream As New System.IO.StreamReader("schema.xsd")
Dim dataSet As DataSet = New DataSet  
dataSet.ReadXmlSchema(xmlStream)  
xmlStream.Close()  
System.IO.StreamReader xmlStream = new System.IO.StreamReader("schema.xsd");  
DataSet dataSet = new DataSet();  
dataSet.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, both the ReadXmlSchema method and the ReadXml method with an XmlReadMode of InferSchema would 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 dataSet As DataSet = New DataSet  
dataSet.InferXmlSchema("input_od.xml", New String() {"urn:schemas-microsoft-com:officedata"})  
DataSet dataSet = new DataSet();  
dataSet.InferXmlSchema("input_od.xml", new string[] "urn:schemas-microsoft-com:officedata");  

See also