共用方式為


Restriction Element Binding Support

The .NET Framework provides partial binding support for the <restriction> element.

The only type restriction for which the .NET Framework provides a fully expressive binding is the enumeration of string-based simple types.

Explanation

The XML Schema definition language enables the deriving of new types from base types through both extension and restriction. Extension is always used to derive complex types. Restriction can be used to derive both complex and simple types, although it is typically used for simple types.

While XML schema type extensions always are fully expressed through class definitions in the .NET Framework, the only type restriction for which a binding exists is the enumeration of a simple type, located via the XPath simpleType/restriction/enumeration. Furthermore, the <enumeration> element is bound only when it is applied to string-based simple types.

For all other children of the simpleType/restriction element (or when the enumeration is applied to a non-string base type), the following is true:

  • When generating source code from an XML Schema document, Xsd.exe ignores the restriction; it simply produces the .NET Framework type corresponding to the base simple type.

  • The XmlSerializer class disregards the restriction when serializing objects to XML and deserializing from XML.

For a complex type, if the restriction involves complex content, via the XPath complexType/complexContent/restriction, Xsd.exe produces an empty class that inherits from a base class. The XmlSerializer class disregards the restriction. Xsd.exe does not recognize a restriction involving simple content, via the XPath complexType/simpleContent/restriction.

While restriction has limited binding support, it is fully represented in the Schema Object Model. There is the XmlSchemaSimpleTypeRestriction class for simple type restriction, and there are the XmlSchemaComplexContentRestriction and XmlSchemaSimpleContentRestriction classes for both kinds of complex type restriction. Furthermore, XmlSchemaSimpleTypeRestriction and XmlSchemaSimpleContentRestriction both have a Facets property that can hold objects representing any of the children of simpleType/restriction or simpleContent/restriction.

Objects of these classes can be used to programmatically create a schema object model that employs any kind of type restriction offered in the XML Schema specification. Such a model can be serialized to and deserialized from an XML Schema document.

In addition, an XmlValidatingReader object can be used to validate an XML instance document against an XML schema that uses restriction.

Possible Attributes Binding Support

base

The <restriction> element requires a base attribute, whose value is the qualified name of the base data type.

For defining simple types via restriction, the value of the base attribute determines whether Xsd.exe recognizes the restriction. If the base is an XML Schema data type that binds to a string, Xsd.exe produces an enum type. Otherwise, Xsd.exe produces a field of the .NET Framework type that binds to the base value.

See the preceding text for the use of the base attribute when defining complex types via restriction.

id

The Xsd.exe utility ignores the id attribute, which is intended to provide a unique identifier.

Possible parent elements: <complexContent>, <simpleContent>, <simpleType>

Possible child elements: <all>, <annotation>, <anyAttribute>, <attribute>, <attributeGroup>, <choice>, <enumeration>, <fractionDigits>, <group>, <length>, <maxExclusive>, <maxInclusive>, <maxLength>, <minExclusive>, <minInclusive>, <minLength>, <pattern>, <sequence>, <simpleType>, <totalDigits>, <whiteSpace>

See Also

Reference

XmlSchemaComplexContentRestriction
XmlSchemaSimpleContentRestriction
XmlSchemaSimpleTypeRestriction