XmlRootAttribute.IsNullable Tulajdonság
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Lekéri vagy beállít egy értéket, amely jelzi, hogy az XmlSerializer adott tagnak szerializálnia null kell-e egy olyan tagotxsi:nil, amely a true következő attribútumra van állítva.
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
Tulajdonság értéke
trueha a XmlSerializer rendszer létrehozza az xsi:nil attribútumot; ellenkező esetben. false
Példák
Az alábbi példa szerializál egy osztályt Group. A példa az osztályra alkalmazza a XmlRootAttribute tulajdonságot, és a tulajdonságot a IsNullable következőre falseállítja be: .
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
Megjegyzések
A struktúrák XML-sémaspecifikációja lehetővé teszi, hogy egy XML-dokumentum explicit módon jelezhesse, hogy egy elem tartalma hiányzik. Az ilyen elem a következő attribútumot xsi:niltruetartalmazza: . További információ: XML-séma 1. rész: Structures Second Edition.
Ha a IsNullable tulajdonság értéke truea következő, akkor az xsi:nil attribútum a következő XML-fájlban látható módon jön létre:
<?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" />
Ha a IsNullable tulajdonság az false, egy üres elem jön létre az alábbi kódban látható módon:
<?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" />