XmlArrayItemAttribute.IsNullable Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene o imposta un valore che indica se deve XmlSerializer serializzare un membro come tag XML vuoto con l'attributo xsi:nil impostato su 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
Valore della proprietà
true
XmlSerializer se genera l'attributoxsi:nil; in caso contrario, falsee non viene generata alcuna istanza. Il valore predefinito è true.
Esempio
Nell'esempio seguente viene serializzata una classe denominata Group, che contiene un campo denominato Employees che restituisce una matrice di Employee oggetti . Una seconda classe denominata Manager deriva da Employee. Specifica XmlArrayItemAttribute che XmlSerializer può inserire entrambi Employee gli oggetti e Manager nella matrice. Nell'esempio viene impostata la IsNullable proprietà , in modo da indicare all'oggetto di XmlSerializer non generare gli xsi:nil oggetti attributo nella matrice impostata su null.
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
Commenti
La specifica di XML Schema per le strutture consente a un documento XML di segnalare in modo esplicito che il contenuto di un elemento non è presente. Tale elemento contiene l'attributo xsi:nil impostato su true. Per altre informazioni, vedere la specifica del World Wide Web Consortium intitolata XML Schema Part 1: Structures.
Se la IsNullable proprietà è true, l'attributo xsi:nil viene generato per i membri della classe che sono stati impostati su null. Ad esempio, se si imposta un campo denominato su MyStringArraynull, viene XmlSerializer generato il codice XML seguente.
<MyStringArray xsi:nil = "true" />
Se la IsNullable proprietà è false, non viene generato alcun elemento XML.
Annotazioni
Non è possibile applicare la IsNullable proprietà a un membro tipizzato come tipo valore perché un tipo valore non può contenere null.