Freigeben über


Fixed Attribute Binding Support 

The .NET Framework provides binding support for the fixed attribute.

Explanation

The fixed attribute can appear in an <element> or <attribute> declaration to establish a constant value that the element or attribute must have in a conforming XML document. The attribute can also appear with any restriction facet element except <enumeration> and <pattern>, in which case a value of true prevents derivation from changing the accompanying facet value.

The .NET Framework does not incorporate restriction facets for data type binding or serialization (with the exception of string-based enumeration), so the fixed attribute is bypassed along with the facet it appears in. The Schema Object Model provides a base XmlSchemaFacet class with an IsFixed property.

If the fixed attribute appears in an <element> or <attribute> element, Xsd.exe statically initializes the field to the default value, as in the following example:

public int age = -1;

According to the XML Schema, the fixed attribute's value must be an XML Schema simple type. For details on how Xsd.exe translates fixed/default values for simple types, see the default attribute.

For elements and attributes, the Schema Object Model represents the fixed attribute with the FixedValue property of XmlSchemaAttribute and XmlSchemaElement classes.

Example

Input XML Schema document:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            targetNamespace="http://example.org/" xmlns="http://example.org/" elementFormDefault="qualified">
    <xsd:element name="FamilyDog" type="FamilyDogType"/>

    <xsd:complexType name="FamilyDogType">
        <xsd:sequence>
            <xsd:element name="name" type="xsd:string" fixed="Spot"/>
            <xsd:element name="birthdate" type="xsd:date" />
        </xsd:sequence>
        <xsd:attribute name="gender" type="GenderType" fixed="UNKNOWN"/>
        <xsd:attribute name="fixed" type="xsd:boolean" fixed="false"/>
        <xsd:attribute name="breed" type="xsd:string" fixed="Swedish Vallhund"/>
    </xsd:complexType>
    
    <xsd:simpleType name="GenderType">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="FEMALE" />
            <xsd:enumeration value="MALE" />
            <xsd:enumeration value="UNKNOWN" />
        </xsd:restriction>
    </xsd:simpleType>
</xsd:schema>

C# classes generated from the preceding XML Schema document:

[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
[System.Xml.Serialization.XmlRootAttribute("FamilyDog", Namespace="http://example.org/", IsNullable=false)]
 public class FamilyDogType {
        
     public string name = "Spot";
        
    [System.Xml.Serialization.XmlElementAttribute(DataType="date")]
    public System.DateTime birthdate;
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public GenderType gender = GenderType.UNKNOWN;
        
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool genderSpecified;
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool @fixed = false;
        
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool fixedSpecified;
        
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string breed = "Swedish Vallhund";
    
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://example.org/")]
public enum GenderType {        
    FEMALE,    
    MALE,
    UNKNOWN,
}

XML Schema document generated from an assembly compiled from the preceding C# source:

<xs:schema xmlns:tns="http://example.org/" elementFormDefault="qualified" targetNamespace="http://example.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="FamilyDog" type="tns:FamilyDogType" />
  <xs:complexType name="FamilyDogType">
    <xs:sequence>
      <xs:element minOccurs="0" maxOccurs="1" name="name" type="xs:string" fixed="Spot"/>
      <xs:element minOccurs="1" maxOccurs="1" name="birthdate" type="xs:date" />
    </xs:sequence>
    <xs:attribute name="gender" type="tns:GenderType" />
    <xs:attribute name="fixed" type="xs:boolean" />
    <xs:attribute name="breed" type="xs:string" />
  </xs:complexType>
  <xs:simpleType name="GenderType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="FEMALE" />
      <xs:enumeration value="MALE" />
      <xs:enumeration value="UNKNOWN" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

Possible containing elements: <attribute>, <element>, various restriction facets

See Also

Reference

XmlSchemaAttribute.FixedValue
XmlSchemaElement.FixedValue
XmlSchemaFacet.IsFixed