XmlRootAttribute.IsNullable Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau menetapkan nilai yang menunjukkan apakah XmlSerializer harus membuat serial anggota yang diatur ke null dalam atribut yang xsi:nil diatur ke true.
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
Nilai Properti
true
XmlSerializer jika menghasilkan xsi:nil atribut; jika tidak, false.
Contoh
Contoh berikut menserialisasi kelas bernama Group. Contoh menerapkan ke XmlRootAttribute kelas , dan mengatur IsNullable properti ke false.
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
Keterangan
Spesifikasi skema XML untuk struktur memungkinkan dokumen XML untuk secara eksplisit memberi sinyal bahwa konten elemen hilang. Elemen seperti itu berisi atribut xsi:nil yang diatur ke true. Untuk informasi selengkapnya, lihat Skema XML Bagian 1: Struktur Edisi Kedua.
IsNullable Jika properti diatur ke true, xsi:nil atribut dihasilkan seperti yang ditunjukkan dalam XML berikut:
<?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 Jika properti adalah false, elemen kosong dibuat seperti yang ditunjukkan dalam kode berikut:
<?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" />