XmlRootAttribute.IsNullable 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定值,指出 XmlSerializer 是否必須將設為 null
的成員序列化成設為 true
的 xsi:nil
屬性。
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
屬性值
如果 XmlSerializer 產生 true
屬性,則為 xsi:nil
,否則為 false
。
範例
下列範例會序列化名為 的 Group
類別。 此範例會將 XmlRootAttribute 套用至 類別,並將 屬性設定 IsNullable 為 false
。
#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 檔明確表示元素的內容遺失。 這類專案包含設定為 true
的屬性 xsi:nil
。 如需詳細資訊,請參閱 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" />