共用方式為


Nillable 屬性繫結支援

.NET Framework 會提供 nillable 屬性的部分繫結支援。

說明

<element> 宣告中的 nillable 屬性設定為 true 時,xsi:nil 屬性就可以出現在 XML 執行個體文件的相符項目中 (前置詞 xsi 通常會用於 XML 結構描述執行個體命名空間 http://www.w3.org/2001/XMLSchema-instance)。

在 XML 項目中,xsi:nil 屬性的 true 值會明確地指定項目缺少內容,無論是子項目或本文都一樣。XmlSerializer 類別相當於具有 null 參考之 nil 屬性的 true 值 (在 Visual Basic 中為 Nothing)。如需 XmlSerializer 類別如何序列化和還原序列化 nil 屬性 (包括特殊情況),請參閱 xsi:nil 屬性。

如果項目宣告為具有 nillable="false" 或沒有 nillable 屬性,則無論 nil 屬性值為何,此屬性都不會有效地出現在對應的執行個體項目中。

從程式碼產生 nillable 屬性

從組件的一組類別產生 XML 結構描述文件時,如果型別是可為 null 的型別 (Nullable Type),Xsd.exe 會為實值型別 (Value Type) 的成員建立 nillable 屬性。這是因為實值型別可以設定為 null 參考 (Nothing)。

對於參考型別的物件,Xsd.exe 會檢查特定 XML 相關屬性 (Attribute) 的 IsNullable 屬性 (Property) 值。如果 IsNullable 屬性 (Property) 設定為 true 值,則 Xsd.exe 會建立 nillable 屬性 (Attribute),並將值設定為 trueIsNullable 屬性是由特定 XML 相關屬性類別使用。屬性顯示如下:

範例用法如下:

[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public string NameNullable;

Xsd.exe 會從這個程式碼產生下列 XSD 項目宣告:

<xs:element minOccurs="1" maxOccurs="1" name="NameNullable" nillable="true" type="xs:string" />

如果 IsNullable 屬性 (Property) 設定為 true,而且某個適用的物件已在執行階段設定為 null 參考 (Nothing),則 XmlSerializer 類別會產生具有 true 值的 xsi:nil 屬性 (Attribute)。

對於套用至可為 null 之實值型別的屬性 (Attribute),IsNullable 屬性 (Property) 必須設為 true

如果是參考型別,IsNullable 屬性 (Property) 值也會決定 <element> 項目的 minOccurs 屬性 (Attribute) 值。IsNullabletrue 值會產生值為 1minOccurs,否則產生的值為 0。請參閱 minOccurs 屬性。

IsNullable 屬性未明確地設定為 true 時,在特定其他情況下,Xsd.exe 也會在 <element> 宣告中產生 nillable="true" 設定。

從 nillable 屬性產生程式碼

在遇到 nillable="true" 設定時,Xsd.exe 會針對可套用至所產生類別或成員的適當 XML 相關屬性產生 IsNullable=true 參數。對於實值型別,則會產生可為 null 的型別。

可能的包含項目:<element>

請參閱

參考

Xsi:nil 屬性繫結支援
System.Xml.Schema.XmlSchemaElement.IsNillable

Footer image

Copyright © 2007 by Microsoft Corporation. All rights reserved.