편집

다음을 통해 공유


XmlSchemaInference.TypeInference Property

Definition

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

public:
 property System::Xml::Schema::XmlSchemaInference::InferenceOption TypeInference { System::Xml::Schema::XmlSchemaInference::InferenceOption get(); void set(System::Xml::Schema::XmlSchemaInference::InferenceOption value); };
public System.Xml.Schema.XmlSchemaInference.InferenceOption TypeInference { get; set; }
member this.TypeInference : System.Xml.Schema.XmlSchemaInference.InferenceOption with get, set
Public Property TypeInference As XmlSchemaInference.InferenceOption

Property Value

An XmlSchemaInference.InferenceOption object.

Examples

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 = gcnew XmlSchemaSet();
XmlSchemaInference^ schema = gcnew XmlSchemaInference();

schema->TypeInference = XmlSchemaInference::InferenceOption::Relaxed;

schemaSet = schema->InferSchema(reader);

for each (XmlSchema^ s in schemaSet->Schemas())
{
    s->Write(Console::Out);
}
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);
}
Dim reader As XmlReader = XmlReader.Create("input.xml")
Dim schemaSet As XmlSchemaSet = New XmlSchemaSet()
Dim schema As XmlSchemaInference = New XmlSchemaInference()

schema.TypeInference = XmlSchemaInference.InferenceOption.Relaxed

schemaSet = schema.InferSchema(reader)

For Each s As XmlSchema In schemaSet.Schemas()
    s.Write(Console.Out)
Next

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>

Remarks

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.

Applies to