Export (0) Print
Expand All

XmlSchemaInference.TypeInference Property

Gets or sets the XmlSchemaInference.InferenceOption value that affects types inferred from the XML document.

Namespace:  System.Xml.Schema
Assembly:  System.Xml (in System.Xml.dll)

public XmlSchemaInference..::.InferenceOption TypeInference { get; set; }

If the TypeInference property is set to Relaxed, the inferred type of elements and attributes in the XML document with simple content is always xs:string. If the TypeInference property is set to Restricted, more specific types are inferred, such as xs:date, xs:decimal, xs:unsignedByte, and so on.

The default value of the TypeInference property is Restricted.

This example illustrates how type inference is affected by the TypeInference property. The example code infers types from an XML file in two different ways: relaxed and restricted. The following is the example XML file.

<?xml version="1.0"?>
<root>
    <subElement1>ABC</subElement1>
    <subElement2>123</subElement2>
</root>

The following example code instructs the XmlSchemaInference class to infer xs:string for elements and attributes with simple content.

XmlReader reader = XmlReader.Create("input.xml");
XmlSchemaSet schemaSet = new XmlSchemaSet();
XmlSchemaInference schema = new XmlSchemaInference();

schema.TypeInference = XmlSchemaInference.InferenceOption.Relaxed;

schemaSet = schema.InferSchema(reader);

foreach (XmlSchema s in schemaSet.Schemas())
{
    s.Write(Console.Out);
}

Because the TypeInference property was set to Relaxed, the following schema was generated.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="subElement1" type="xs:string" />
                <xs:element name="subElement2" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

In the example code above, if the TypeInference property was not set to Relaxed, the XmlSchemaInference class would have defaulted to Restricted and generated the following schema.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="subElement1" type="xs:string" />
                <xs:element name="subElement2" type="xs:unsignedByte" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft