共用方式為


XmlArrayItemAttribute.IsNullable 屬性

定義

取得或設定值,指出 XmlSerializer 是否必須將成員序列化為 xsi:nil 屬性設為 true 的空 XML 標記。

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

屬性值

Boolean

如果 XmlSerializer 產生 true 屬性,則為 xsi:nil,否則為 false,而且不會產生執行個體。 預設為 true

範例

下列範例會序列化名為 Group 的類別,其中包含名為 EmployeesEmployee 欄位,該欄位會傳回 物件的陣列。 名為 Manager 的第二個類別衍生自 Employee 。 指定 XmlArrayItemAttribute XmlSerializer 可以將 和 Manager 物件插入 Employee 陣列中。 此範例會 IsNullable 設定 屬性,藉此告知 XmlSerializer 不要在陣列中將 xsi:nil 屬性物件產生為 null

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

public ref class Employee
{
public:
   String^ Name;
};

public ref class Manager: public Employee
{
public:
   int Level;
};

public ref class Group
{
public:

   [XmlArray(IsNullable=true)]
   [XmlArrayItem(Manager::typeid,IsNullable=false),
   XmlArrayItem(Employee::typeid,IsNullable=false)]
   array<Employee^>^Employees;
};

void SerializeObject( String^ filename )
{
   XmlSerializer^ s = gcnew XmlSerializer( Group::typeid );

   // To write the file, a TextWriter is required.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Creates the object to serialize.
   Group^ group = gcnew Group;

   // Creates a null Manager object.
   Manager^ mgr = nullptr;

   // Creates a null Employee object.
   Employee^ y = nullptr;
   array<Employee^>^temp = {mgr,y};
   group->Employees = temp;

   // Serializes the object and closes the TextWriter.
   s->Serialize( writer, group );
   writer->Close();
}

int main()
{
   SerializeObject( "TypeDoc.xml" );
}
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 。 如需詳細資訊,請參閱標題為 XML 架構第 1 部分:結構的萬維網聯盟規格。

IsNullable如果 屬性為 true ,則會針對已設定 null 為 的 xsi:nil 類別成員產生 屬性。 例如,如果您將名為 MyStringArray 的欄位設定為 null ,就會 XmlSerializer 產生下列 XML 程式碼。

<MyStringArray xsi:nil = "true" />  

IsNullable如果 屬性為 false ,則不會產生任何 XML 專案。

注意

您無法將 IsNullable 屬性套用至型別為實值型別的成員,因為實值型別不能包含 null

適用於