Fixed Attribute Binding Support

This topic is specific to a legacy technology. XML Web services and XML Web service clients should now be created using Windows Communication Foundation.

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


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.


Input XML Schema document:

<xsd:schema xmlns:xsd="" 
            targetNamespace="" xmlns="" elementFormDefault="qualified">
    <xsd:element name="FamilyDog" type="FamilyDogType"/>

    <xsd:complexType name="FamilyDogType">
            <xsd:element name="name" type="xsd:string" fixed="Spot"/>
            <xsd:element name="birthdate" type="xsd:date" />
        <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:simpleType name="GenderType">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="FEMALE" />
            <xsd:enumeration value="MALE" />
            <xsd:enumeration value="UNKNOWN" />

C# classes generated from the preceding XML Schema document:

[System.Xml.Serialization.XmlRootAttribute("FamilyDog", Namespace="", IsNullable=false)]
 public class FamilyDogType {
     public string name = "Spot";
    public System.DateTime birthdate;
    public GenderType gender = GenderType.UNKNOWN;
    public bool genderSpecified;
    public bool @fixed = false;
    public bool fixedSpecified;
    public string breed = "Swedish Vallhund";
public enum GenderType {        

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

<xs:schema xmlns:tns="" elementFormDefault="qualified" targetNamespace="" xmlns:xs="">
  <xs:element name="FamilyDog" type="tns:FamilyDogType" />
  <xs:complexType name="FamilyDogType">
      <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:attribute name="gender" type="tns:GenderType" />
    <xs:attribute name="fixed" type="xs:boolean" />
    <xs:attribute name="breed" type="xs:string" />
  <xs:simpleType name="GenderType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="FEMALE" />
      <xs:enumeration value="MALE" />
      <xs:enumeration value="UNKNOWN" />

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

See Also