XmlArrayItemAttribute.IsNullable 속성
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
특성true이 설정된 빈 XML 태그 xsi:nil 로 멤버를 serialize해야 하는지 여부를 XmlSerializer 나타내는 값을 가져오거나 설정합니다.
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
속성 값
true입니다.
예제
다음 예제에서는 개체 배열 Employee 을 반환하는 이름이 지정된 필드를 포함하는 명명 GroupEmployees 된 클래스를 serialize합니다. 명명된 Manager 두 번째 클래스는 .에서 Employee파생됩니다. 배열 XmlArrayItemAttribute 에 개체와 Manager 개체를 모두 Employee 삽입할 수 있도록 XmlSerializer 지정합니다. 이 예제에서는 속성을 설정 IsNullable 하므로 배열 집합null에서 XmlSerializer 특성 개체를 xsi:nil 생성하지 말라고 합니다.
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 컨소시엄 사양을 참조하세요.
속성이 IsNullabletruexsi:nil 면 설정된 클래스 멤버에 대한 특성이 null생성됩니다. 예를 들어 이름이 지정된 MyStringArraynullXmlSerializer 필드를 설정하면 다음 XML 코드가 생성됩니다.
<MyStringArray xsi:nil = "true" />
속성이 IsNullablefalse면 XML 요소가 생성되지 않습니다.
메모
값 형식에 IsNullable 포함 null할 수 없으므로 값 형식으로 형식화된 멤버에는 속성을 적용할 수 없습니다.