XmlArrayItemAttribute.IsNullable Właściwość

Definicja

Pobiera lub ustawia wartość wskazującą, czy XmlSerializer element członkowski musi serializować jako pusty tag XML z atrybutem ustawionym xsi:nil na truewartość .

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

Wartość właściwości

Boolean

trueXmlSerializer jeśli element generuje xsi:nil atrybut ; w przeciwnym razie false, i nie jest generowane żadne wystąpienie. Wartość domyślna to true.

Przykłady

Poniższy przykład serializuje klasę o nazwie , która zawiera pole o Groupnazwie Employees , które zwraca tablicę Employee obiektów. Druga klasa o nazwie Manager pochodzi z klasy Employee. Element XmlArrayItemAttribute określa, że XmlSerializer obiekt może wstawić obiekty i Employee Manager do tablicy. W przykładzie ustawiono IsNullable właściwość , co oznacza XmlSerializer , że nie należy generować xsi:nil obiektów atrybutów w tablicy ustawionej na nullwartość .

#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

Uwagi

Specyfikacja schematu XML dla struktur umożliwia dokumentowi XML jawne sygnalizowanie braku zawartości elementu. Taki element zawiera atrybut xsi:nil ustawiony na true. Aby uzyskać więcej informacji, zobacz specyfikację konsorcjum World Wide Web Consortium zatytułowaną XML Schema Part 1: Structures.

IsNullable Jeśli właściwość ma truewartość , xsi:nil atrybut jest generowany dla składowych klasy, które zostały ustawione na null. Jeśli na przykład ustawisz pole o nazwie MyStringArray na null, XmlSerializer polecenie wygeneruje następujący kod XML.

<MyStringArray xsi:nil = "true" />  

IsNullable Jeśli właściwość ma falsewartość , nie jest generowany żaden element XML.

Uwaga

Nie można zastosować IsNullable właściwości do elementu członkowskiego wpisanego jako typ wartości, ponieważ typ wartości nie może zawierać nullelementu .

Dotyczy