共用方式為


Default 屬性繫結支援

.NET Framework 會提供 default 屬性 (Attribute) 的部分繫結支援。

Xsd.exe 工具等同於具有已套用至所產生欄位之 System.ComponentModel.DefaultValueAttribute 值的預設項目或屬性;而且該欄位也會靜態地初始化為預設值。

說明

如果收到執行個體文件時,項目是空的或屬性不存在,可在 <element><attribute> 宣告中出現的 default 屬性便會提供要使用的預設值。

對屬性而言,只有當屬性為選擇項 (也就是,Attribute 項目繫結支援中所述的 use 屬性具有預設值 optional),預設值才是適用的值。指定屬性時,必須與值一起指定。

對項目而言,只有當項目出現在沒有任何內容的執行個體文件中時,才會使用預設值。如果項目根本沒有出現,這個屬性就不會有值。

從 XML 結構描述文件產生原始程式碼時,Xsd.exe 工具會讓每個欄位都對應到具有預設值的項目或屬性,並套用 System.ComponentModel.DefaultValueAttribute,以傳遞預設值當做引數。此外,Xsd.exe 會將欄位靜態初始化為預設值,如下列範例所示:

[System.ComponentModel.DefaultValueAttribute(-1)]
public int age = -1;

在陣列 (項目的 maxOccurs 大於 1) 上會忽略預設的屬性。

資料型別提供的繫結支援

根據 XML 結構描述,default 屬性的值必須是簡單型別。對於幾乎所有簡單型別而言,當 Xsd.exe 從 XML 結構描述轉譯至類別,然後又轉譯回到新 XML 結構描述文件時,會保留預設值。

但是型別 xsd:base64Binaryxsd:hexBinary 的項目或屬性例外。DefaultValue 屬性不會套用至所建立的對應 System.Byte 欄位。

另一個例外狀況則是清單型別的屬性。<list> 建構是用來定義簡單型別,而這個簡單型別的可能值為一系列來自其他簡單型別且以空白分隔的值。(Xsd.exe 不會針對清單型別的項目產生原始程式碼)。就清單而言,Xsd.exe 會建立構成型別的陣列,但是也會將預設值轉換為構成型別的執行個體,而非陣列。

請考慮下列輸入 <attribute> 宣告:

<xsd:attribute name="siblings" default="unknown">
  <xsd:simpleType>
    <xsd:list itemType="xsd:string"/>
  </xsd:simpleType>
</xsd:attribute>

如果是這項宣告,則會產生下列原始程式碼:

[System.Xml.Serialization.XmlAttributeAttribute()]
[System.ComponentModel.DefaultValueAttribute("unknown")]
public string[] siblings = "unknown";

此原始檔無法編譯,因為 String 物件無法以隱含方式型別轉換型別為 String 物件的陣列。任何簡單型別的清單都會發生同樣的錯誤,包括列舉型別。

項目繫結實值型別 (Value Type) 的 minOccurs 屬性上的效果

假設類別成員具有 .NET Framework 實值型別,並且對應到 XML 項目 (透過預設的 XmlElementAttribute)。從類別產生 XSD 文件時,如果 Xsd.exe 遇到已套用至這類成員的 DefaultValue 屬性,就會針對 <element> 項目的 minOccurs 屬性產生 0 值。這表示此項目不需要出現在有效的 XML 執行個體文件中。

配合 use="optional" 的屬性組合

假設 <attribute> 宣告包含值為 optionaluse 屬性,此值同時也是預設值。從 XSD 文件產生原始程式碼時,Xsd.exe 會視是否已指定 default 屬性來解譯此值。兩種可能的解譯為:

  • 已指定 default:Xsd.exe 會使用 XmlAttributeAttribute 加上指定預設值的 DefaultValueAttribute,來產生公用欄位。

  • 未指定 default:Xsd.exe 會使用 XmlAttributeAttribute 來產生公用欄位。此外,它還會針對實值型別產生型別 bool 的公用欄位,該欄位的名稱為屬性欄位的名稱加上 Specified。例如,如果屬性欄位的名稱為 startDatebool 欄位的名稱就會變成 startDateSpecified。將物件序列化為 XML 時,XmlSerializer 類別會檢查 bool 欄位的值,以判斷是否寫入選擇性屬性。bool 欄位會和 System.Xml.Serialization.XmlIgnoreAttribute 一起顯示,以避免被 XmlSerializer 序列化。

範例

輸入 XML 結構描述文件:

<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:stringo" default="Spot"/>
            <xsd:element name="birthdate" type="xsd:date" default="2002-03-04"/>
        </xsd:sequence>
        <xsd:attribute name="gender" type="GenderType" default="UNKNOWN"/>
        <xsd:attribute name="fixed" type="xsd:boolean" default="false"/>
        <xsd:attribute name="breed" type="xsd:string" default="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>

由前面的 XML 結構描述文件所產生的 C# 類別:

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

從前面 C# 原始程式碼所編譯的組件而產生的 XML 結構描述文件,會有效地與產生 C# 原始檔的原始 XML 結構描述一樣。

可能的包含項目:<attribute><element>

請參閱

參考

System.Xml.Schema.XmlSchemaAttribute.DefaultValue
System.Xml.Schema.XmlSchemaElement.DefaultValue

Footer image

Copyright © 2007 by Microsoft Corporation. All rights reserved.