Aracılığıyla paylaş


XmlArrayItemAttribute.IsNullable Özellik

Tanım

öğesinin, özniteliği olarak ayarlanmış trueboş bir XML etiketi olarak bir üyeyi seri hale getirmesi xsi:nil gerekip gerekmediğini XmlSerializer belirten bir değer alır veya ayarlar.

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

Özellik Değeri

true özniteliğini XmlSerializer oluşturursa xsi:nil , aksi takdirde false, ve hiçbir örnek oluşturulmaz. Varsayılan değer: true.

Örnekler

Aşağıdaki örnek, adlı Groupbir sınıfı serileştirir. Bu sınıf, bir nesne dizisi Employee döndüren adlı Employees bir alan içerir. adlı Manager ikinci sınıf öğesinden Employeetüretilir. , XmlArrayItemAttribute hem XmlSerializer hem Manager de Employee nesnelerini diziye ekleyebileceğini belirtir. Örnek özelliğini ayarlarIsNullable, böylece dizideki öznitelik nesnelerinin olarak xsi:nil ayarlanmaması nullgerektiğini söylerXmlSerializer.

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

Açıklamalar

Yapıların XML şema belirtimi, XML belgesinin bir öğenin içeriğinin eksik olduğunu açıkça belirtmesine olanak tanır. Böyle bir öğe olarak trueayarlanmış özniteliğini xsi:nil içerir. Daha fazla bilgi için bkz. XML Şema Bölüm 1: Yapılar başlıklı World Wide Web Konsorsiyumu belirtimi.

IsNullable özelliği isetruexsi:nil, özniteliği olarak ayarlanmış nullsınıf üyeleri için oluşturulur. Örneğin, adlı MyStringArraynullbir alan ayarlarsanız, aşağıdaki XmlSerializer XML kodunu oluşturur.

<MyStringArray xsi:nil = "true" />

IsNullable özelliği isefalse, XML öğesi oluşturulmaz.

Uyarı

Bir değer türü içeremediğindennull, özelliğini değer türü olarak yazılan üyeye uygulayamazsınızIsNullable.

Şunlara uygulanır