Bagikan melalui


XmlArrayItemAttribute.IsNullable Properti

Definisi

Mendapatkan atau menetapkan nilai yang menunjukkan apakah XmlSerializer harus menserialisasikan anggota sebagai tag XML kosong dengan atribut yang xsi:nil diatur ke 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

Nilai Properti

true XmlSerializer jika menghasilkan xsi:nil atribut; jika tidak, false, dan tidak ada instans yang dihasilkan. Defaultnya adalah true.

Contoh

Contoh berikut menserialisasikan kelas bernama Group, yang berisi bidang bernama Employees yang mengembalikan array Employee objek. Kelas kedua bernama Manager berasal dari Employee. Menentukan XmlArrayItemAttribute bahwa XmlSerializer dapat menyisipkan objek Employee dan Manager ke dalam array. Contoh mengatur IsNullable properti, sehingga memberi tahu XmlSerializer untuk tidak menghasilkan xsi:nil objek atribut dalam array yang diatur ke 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

Keterangan

Spesifikasi skema XML untuk struktur memungkinkan dokumen XML untuk secara eksplisit memberi sinyal bahwa konten elemen hilang. Elemen seperti itu berisi atribut xsi:nil yang diatur ke true. Untuk informasi selengkapnya, lihat spesifikasi World Wide Web Consortium berjudul Skema XML Bagian 1: Struktur.

IsNullable Jika properti adalah true, xsi:nil atribut dihasilkan untuk anggota kelas yang telah diatur ke null. Misalnya, jika Anda mengatur bidang bernama MyStringArray ke null, XmlSerializer menghasilkan kode XML berikut.

<MyStringArray xsi:nil = "true" />

IsNullable Jika properti adalah false, tidak ada elemen XML yang dihasilkan.

Nota

Anda tidak dapat menerapkan properti ke IsNullable anggota yang dititik sebagai tipe nilai karena tipe nilai tidak boleh berisi null.

Berlaku untuk