XmlArrayItemAttribute.IsNullable 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定一個值,指示是否 XmlSerializer 必須將成員序列化為空的 XML 標籤,屬性 xsi:nil 設為 true。
public:
property bool IsNullable { bool get(); void set(bool value); };
public bool IsNullable { get; set; }
member this.IsNullable : bool with get, set
Public Property IsNullable As Boolean
屬性值
true 若 產生 XmlSerializer 屬性 xsi:nil ;否則 , false則不產生實例。 預設值為 true。
範例
以下範例序列化一個名為 Group的類別,該類別包含一個名為 Employees 的欄位,該欄位回傳一個物件陣列 Employee 。 第二類稱為 Manager ,源自 Employee。 A XmlArrayItemAttribute 指定 可以 XmlSerializer 將 和 EmployeeManager 兩個物件都插入到陣列中。 範例設定屬性 IsNullable ,指示 不 XmlSerializer 產生 xsi:nil 陣列中屬性 null物件。
using System;
using System.IO;
using System.Xml.Serialization;
public class Group
{
[XmlArray(IsNullable = true)]
[XmlArrayItem(typeof(Manager), IsNullable = false),
XmlArrayItem(typeof(Employee), IsNullable = false)]
public Employee[] Employees;
}
public class Employee
{
public string Name;
}
public class Manager:Employee
{
public int Level;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("TypeDoc.xml");
}
public void SerializeObject(string filename)
{
XmlSerializer s = new XmlSerializer(typeof(Group));
// To write the file, a TextWriter is required.
TextWriter writer = new StreamWriter(filename);
// Creates the object to serialize.
Group group = new Group();
// Creates a null Manager object.
Manager mgr = null;
// Creates a null Employee object.
Employee y = null;
group.Employees = new Employee[2] {mgr, y};
// Serializes the object and closes the TextWriter.
s.Serialize(writer, group);
writer.Close();
}
}
Option Explicit
Option Strict
Imports System.IO
Imports System.Xml.Serialization
Public Class Group
<XmlArray(IsNullable := True), _
XmlArrayItem(GetType(Manager), IsNullable := False), _
XmlArrayItem(GetType(Employee), IsNullable := False)> _
Public Employees() As Employee
End Class
Public Class Employee
Public Name As String
End Class
Public Class Manager
Inherits Employee
Public Level As Integer
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeObject("TypeDoc.xml")
End Sub
Public Sub SerializeObject(filename As String)
Dim s As New XmlSerializer(GetType(Group))
' To write the file, a TextWriter is required.
Dim writer As New StreamWriter(filename)
' Creates the object to serialize.
Dim group As New Group()
' Creates a null Manager object.
Dim mgr As Manager = Nothing
' Creates a null Employee object.
Dim y As Employee = Nothing
group.Employees = New Employee() {mgr, y}
' Serializes the object and closes the TextWriter.
s.Serialize(writer, group)
writer.Close()
End Sub
End Class
備註
結構的 XML 結構規範允許 XML 文件明確表示元素內容缺失。 此類元素包含設定為 true的屬性xsi:nil。 欲了解更多資訊,請參閱萬維網聯盟(WW)規範《 XML 架構第一部分:結構》。
若屬性 IsNullable 為 true, xsi:nil 則為已設定為 null的類別成員產生屬性。 例如,如果你將一個名為 MyStringArraynull的欄位設為 ,就會 XmlSerializer 產生以下 XML 程式碼。
<MyStringArray xsi:nil = "true" />
若屬性 IsNullable 為 false,則不會產生 XML 元素。
備註
你無法將該 IsNullable 屬性套用到被定義為值型別的成員身上,因為值型別無法包含 null。