XmlArrayItemAttribute.IsNullable 속성

정의

XmlSerializer가 멤버를 xsi:nil 특성이 true로 설정된 빈 XML 태그로 serialize해야 하는지 여부를 나타내는 값을 가져오거나 설정합니다.

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

XmlSerializertrue 특성을 생성하면 xsi:nil이고, 그렇지 않으면 false이고 인스턴스가 생성되지 않습니다. 기본값은 true입니다.

예제

다음 예제에서는 개체 배열 Employee 을 반환하는 필드가 포함된 명명 Group``Employees 된 클래스를 serialize합니다. 명명된 Manager 두 번째 클래스는 .에서 Employee파생됩니다. 배열 XmlArrayItemAttribute 에 개체와 Manager 개체를 XmlSerializer 모두 Employee 삽입할 수 있도록 지정합니다. 이 예제에서는 속성을 설정하여 배열 집합 IsNullable null에서 XmlSerializer 특성 개체를 xsi:nil 생성하지 말라고 합니다.

#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 문서에서 요소의 콘텐츠가 누락되었음을 명시적으로 알릴 수 있습니다. 이러한 요소에는 .로 설정된 특성 xsi:nil 이 포함됩니다 true. 자세한 내용은 XML 스키마 1부: 구조체라는 World Wide Web 컨소시엄 사양을 참조하세요.

속성이 IsNullable true``xsi:nil 면 설정된 클래스 멤버에 대해 특성이 null생성됩니다. 예를 들어 명명 MyStringArray nullXmlSerializer 된 필드를 설정하면 다음 XML 코드가 생성됩니다.

<MyStringArray xsi:nil = "true" />  

속성이 IsNullable false면 XML 요소가 생성되지 않습니다.

참고

값 형식에 IsNullable 포함할 수 없으므로 값 형식으로 형식화된 멤버에는 속성을 적용할 수 없습니다 null.

적용 대상