Supporto dell'associazione all'attributo fixed
In .NET Framework è incluso un supporto per l'associazione all'attributo fixed.
Descrizione
L'attributo fixed può essere visualizzato in una dichiarazione <element> o <attribute> per stabilire un valore costante di cui l'elemento o l'attributo deve disporre in un documento XML conforme. L'attributo può essere inoltre visualizzato con qualsiasi elemento facet di restrizione tranne <enumeration> e <pattern>, nel qual caso il valore true
impedisce che la derivazione modifichi il valore del facet associato.
.NET Framework non include i facet di restrizione per l'associazione dei tipi di dati o la serializzazione, ad eccezione dell'enumerazione basata sulla stringa, e pertanto l'attributo fixed viene ignorato insieme con il facet nel quale viene visualizzato. Il modello SOM (Schema Object Model) offre una classe XmlSchemaFacet base con una proprietà IsFixed.
Se l'attributo fixed viene visualizzato in un elemento <element> o <attributo>, Xsd.exe inizializza in modo statico il campo sul valore predefinito, come nell'esempio riportato di seguito.
public int age = -1;
In base allo schema XML, il valore dell'attributo fixed deve essere un tipo semplice dello schema XML. Per informazioni dettagliate sul modo in cui i valori fissi/predefiniti vengono convertiti per i tipi semplici in Xsd.exe, vedere l'attributo default.
Per gli elementi e gli attributi, il modello SOM (Schema Object Model) rappresenta l'attributo fixed con la proprietà FixedValue delle classi XmlSchemaAttribute e XmlSchemaElement.
Esempio
Documento dello schema XML di input:
<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>
Classi C# generate dal precedente documento dello schema XML:
[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,
}
Documento dello schema XML generato da un assembly compilato dal codice sorgente C# precedente:
<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>
Elementi contenitori possibili: <attribute>, <element>, diversi facet di restrizione
Vedere anche
Riferimenti
System.Xml.Schema.XmlSchemaAttribute.FixedValue
System.Xml.Schema.XmlSchemaElement.FixedValue
System.Xml.Schema.XmlSchemaFacet.IsFixed
Copyright © 2007 Microsoft Corporation. Tutti i diritti riservati.