Muokkaa

Jaa


Inferring Columns

After ADO.NET has determined from an XML document which elements to infer as tables for a DataSet, it then infers the columns for those tables. ADO.NET 2.0 introduced a new schema inference engine that infers a strongly typed data type for each simpleType element. In previous versions, the data type of an inferred simpleType element was always xsd:string.

Migration and Backward Compatibility

The ReadXml method takes an argument of type InferSchema. This argument allows you to specify inference behavior compatible with previous versions. The available values for the InferSchema enumeration are shown in the following table.

InferSchema
Provides backward compatibility by always inferring a simple type as String.

InferTypedSchema
Infers a strongly typed data type. Throws an exception if used with a DataTable.

IgnoreSchema
Ignores any inline schema and reads data into the existing DataSet schema.

Attributes

As defined in Inferring Tables, an element with attributes will be inferred as a table. The attributes of that element will then be inferred as columns for the table. The ColumnMapping property of the columns will be set to MappingType.Attribute, to ensure that the column names will be written as attributes if the schema is written back to XML. The values of the attributes are stored in a row in the table. For example, consider the following XML:

<DocumentElement>  
  <Element1 attr1="value1" attr2="value2"/>  
</DocumentElement>  

The inference process will produce a table named Element1 with two columns, attr1 and attr2. The ColumnMapping property of both columns will be set to MappingType.Attribute.

DataSet: DocumentElement

Table: Element1

attr1 attr2
value1 value2

Elements Without Attributes or Child Elements

If an element has no child elements or attributes, it will be inferred as a column. The ColumnMapping property of the column will be set to MappingType.Element. The text for child elements is stored in a row in the table. For example, consider the following XML:

<DocumentElement>  
  <Element1>  
    <ChildElement1>Text1</ChildElement1>  
    <ChildElement2>Text2</ChildElement2>  
  </Element1>  
</DocumentElement>  

The inference process will produce a table named Element1 with two columns, ChildElement1 and ChildElement2. The ColumnMapping property of both columns will be set to MappingType.Element.

DataSet: DocumentElement

Table: Element1

ChildElement1 ChildElement2
Text1 Text2

See also