แก้ไข

แชร์ผ่าน


XmlSchemaInference.Occurrence Property

Definition

Gets or sets the XmlSchemaInference.InferenceOption value that affects schema occurrence declarations inferred from the XML document.

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

Property Value

An XmlSchemaInference.InferenceOption object.

Examples

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

<?xml version="1.0"?>
<root>
    <subElement1 attribute1="text">ABC</subElement1>
</root>

The following example code instructs the XmlSchemaInference class to infer occurrence of elements and attributes in a relaxed way.

XmlReader^ reader = XmlReader::Create("input.xml");
XmlSchemaSet^ schemaSet = gcnew XmlSchemaSet();
XmlSchemaInference^ schema = gcnew XmlSchemaInference();

schema->Occurrence = 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.Occurrence = 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.Occurrence = XmlSchemaInference.InferenceOption.Relaxed

schemaSet = schema.InferSchema(reader)

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

Because the Occurrence 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 minOccurs="0" name="subElement1">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
                            <xs:attribute name="attribute1" type="xs:string" use="optional"/>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

In the example code above, if the Occurrence 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">
                    <xs:complexType>
                        <xs:simpleContent>
                            <xs:extension base="xs:string">
                                <xs:attribute name="attribute1" type="xs:string" use="required"/>
                            </xs:extension>
                        </xs:simpleContent>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Remarks

If the Occurrence property is set to Restricted, the first time elements are encountered in the XML document, the schema declaration is inferred as minOccurs="1". When attributes are encountered, the schema declaration is inferred as use="required".

If the Occurrence property is set to Relaxed, element schema declarations are inferred as minOccurs="0", and attribute schema declarations are inferred as use="optional".

The default value of the Occurrence property is Restricted.

Applies to