Поделиться через


Value Attribute Binding Support 

The .NET Framework provides partial binding support for the value attribute.

The Xsd.exe tool translates the value attribute of an <enumeration> facet into a .NET Framework enum member if the enumeration restriction is applied to a string-based type. Otherwise, there are no bindings between the XML Schema's facets and class definitions in the .NET Framework.

Explanation

The XML Schema definition language provides various mechanisms for deriving new simple types by constraining the values of the base simple types. These constraints are called facets and are specified as children of the <restriction> element in a <simpleType> (or <simpleContent>) definition, located via the XPath simpleType/restriction/facetName, where facetName is the particular facet. The value attribute of the facet element specifies a value appropriate to the facet.

The value attribute in string-based enumerations

The Xsd.exe tool translates the value attribute of an <enumeration> facet into a .NET Framework enum member if the enumeration restriction is applied to a string-based data type. The data type is specified via the base attribute of the <restriction> element. See the <enumeration> element for a list of built-in XML schema data types that the Xsd.exe tool interprets as strings.

An enumeration value needs to either qualify as a valid constant name or be converted by Xsd.exe into a constant name. An example is the following enumeration value:

<xsd:enumeration value="IRISH CREAM" />

This value is converted into the following enum member:

[System.Xml.Serialization.XmlEnumAttribute("IRISH CREAM")]
IRISHCREAM,

The single space is removed to produce a valid constant name, and an XmlEnumAttribute attribute is applied to the enum member. The attribute parameter changes the XML Schema enumeration value that is used for that enum value. The default is the enum value itself, in this case IRISHCREAM. For an XML Schema enumeration value that already qualifies as a valid constant name, the XmlEnum attribute is omitted.

While the .NET Framework does not translate a numeric XSD enumeration into an enum definition, it does translate a string XSD enumeration whose values happen to be numbers. The following simple type definition binds to an enum type because it specifies base="xsd:string":

<xsd:simpleType name="Primes">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="2" />
        <xsd:enumeration value="3" />
        <xsd:enumeration value="5" />
        <xsd:enumeration value="7" />
        <xsd:enumeration value="11" />
        <xsd:enumeration value="13" />
        <xsd:enumeration value="17" />
    </xsd:restriction>
</xsd:simpleType>

The following enum type is produced:

public enum Primes { 
    [System.Xml.Serialization.XmlEnumAttribute("2")]
    Item2,
    [System.Xml.Serialization.XmlEnumAttribute("3")]
    Item3,
    [System.Xml.Serialization.XmlEnumAttribute("5")]
    Item5,
    [System.Xml.Serialization.XmlEnumAttribute("7")]
    Item7,
    [System.Xml.Serialization.XmlEnumAttribute("11")]
    Item11,
    [System.Xml.Serialization.XmlEnumAttribute("13")]
    Item13,
    [System.Xml.Serialization.XmlEnumAttribute("17")]
    Item17,
}

Again, the XmlEnum attribute is used to override the default binding of the xsd:enumeration value to an XML Schema enumeration value.

Other uses of the value attribute

Aside from enumerations based on string-binding data types, there are no bindings between simple type restrictions using the XML Schema's facets and class definitions in the .NET Framework. This means the following (with the exception of string-based enumerations):

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

  • The XmlSerializer class disregards these restrictions when serializing objects to XML and deserializing from XML.

Concerning the Schema Object Model, the System.Xml.Schema namespace has an abstract XmlSchemaFacet class with an XmlSchemaFacet.Value property. Types derived from XmlSchemaFacet for each of the possible restriction facets can be used to programmatically create a schema object model that restricts via facets.

Possible containing elements: all restriction facets

See Also

Reference

XmlSchemaFacet.Value