XmlRootAttribute.IsNullable 속성

정의

XmlSerializernull로 설정된 멤버를 xsi:nil로 설정된 true 특성으로 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입니다.

예제

다음 예제에서는 라는 클래스를 직렬화 Group합니다. 이 예제에서는 클래스에 XmlRootAttribute 적용하고 속성을 false.로 설정합니다IsNullable.

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

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

// Apply the XmlRootAttribute and set the IsNullable property to false.

[XmlRoot(IsNullable=false)]
public ref class Group
{
public:
   String^ Name;
};

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

   // Writing the file requires a TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Create the object to serialize.
   Group^ mygroup = nullptr;

   // Serialize the object, and close the TextWriter.
   s->Serialize( writer, mygroup );
   writer->Close();
}

int main()
{
   Console::WriteLine( "Running" );
   SerializeObject( "NullDoc.xml" );
}
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

// Apply the XmlRootAttribute and set the IsNullable property to false.
[XmlRoot(IsNullable = false)]
public class Group
{
   public string Name;
}

public class Run
{
   public static void Main()
   {
   Console.WriteLine("Running");
      Run test = new Run();
      test.SerializeObject("NullDoc.xml");
   }

   public void SerializeObject(string filename)
   {
      XmlSerializer s = new XmlSerializer(typeof(Group));

      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create the object to serialize.
      Group mygroup = null;

      // Serialize the object, and close the TextWriter.
      s.Serialize(writer, mygroup);
      writer.Close();
   }
}
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml


' Apply the XmlRootAttribute and set the IsNullable property to false.
<XmlRoot(IsNullable := False)> _
Public Class Group
    Public Name As String
End Class


Public Class Run
    
    Public Shared Sub Main()
        Console.WriteLine("Running")
        Dim test As New Run()
        test.SerializeObject("NullDoc.xml")
    End Sub     
    
    Public Sub SerializeObject(ByVal filename As String)
        Dim s As New XmlSerializer(GetType(Group))
        
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Create the object to serialize.
        Dim mygroup As Group = Nothing
        
        ' Serialize the object, and close the TextWriter.
        s.Serialize(writer, mygroup)
        writer.Close()
    End Sub
End Class

설명

구조체에 대한 XML 스키마 사양을 사용하면 XML 문서에서 요소의 콘텐츠가 누락되었음을 명시적으로 신호를 보낼 수 있습니다. 이러한 요소에는 .로 설정된 특성 xsi:nil 이 포함됩니다 true. 자세한 내용은 XML 스키마 1부: 구조체 두 번째 버전을 참조하세요.

속성이 IsNullable 설정 true``xsi:nil 되면 다음 XML에 표시된 대로 특성이 생성됩니다.

<?xml version="1.0" encoding="utf-8"?>  
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:nil="true" />  

속성이 IsNullable false면 다음 코드와 같이 빈 요소가 만들어집니다.

<?xml version="1.0" encoding="utf-8"?>  
<Group xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
xmlns:xsd="http://www.w3.org/2001/XMLSchema" />  

적용 대상