Freigeben über


XmlArrayItemAttribute.IsNullable-Eigenschaft

Ruft einen Wert ab, der angibt, ob XmlSerializer einen Member als leeres XML-Tag, bei dem das xsi:nil-Attribut auf true festgelegt ist, serialisieren muss, oder legt diesen fest.

Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)

Syntax

'Declaration
Public Property IsNullable As Boolean
'Usage
Dim instance As XmlArrayItemAttribute
Dim value As Boolean

value = instance.IsNullable

instance.IsNullable = value
public bool IsNullable { get; set; }
public:
property bool IsNullable {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_IsNullable ()

/** @property */
public void set_IsNullable (boolean value)
public function get IsNullable () : boolean

public function set IsNullable (value : boolean)

Eigenschaftenwert

true, wenn XmlSerializer das xsi:nil-Attribut generiert, andernfalls false, und es wird keine Instanz generiert. Der Standardwert ist true.

Hinweise

Die XML-Schemaspezifikation für Strukturen ermöglicht es einem XML-Dokument, explizit zu signalisieren, dass der Inhalt eines Elements fehlt. Das Attribut xsi:nil eines solchen Elements ist auf true festgelegt. Weitere Informationen finden Sie in der World Wide Web Consortium-Spezifikation (www.w3.org) "XML Schema Part 1: Structures".

Wenn die IsNullable-Eigenschaft true ist, wird das xsi:nil-Attribut für Klassenmember generiert, die auf NULL (Nothing in Visual Basic) festgelegt wurden. Wenn Sie z. B. das Feld MyStringArray auf NULL (Nothing in Visual Basic) festlegen, generiert XmlSerializer folgenden XML-Code.

 <MyStringArray xsi:nil = "true" />

Wenn die IsNullable-Eigenschaft false ist, wird kein XML-Element generiert.

Hinweis

Sie können die IsNullable-Eigenschaft nicht einem Member zuweisen, der als Werttyp definiert wurde, da ein Werttyp nicht NULL (Nothing in Visual Basic) enthalten kann.

Beispiel

Im folgenden Beispiel wird die Group-Klasse serialisiert, die das Feld Employees enthält, das ein Array von Employee-Objekten zurückgibt. Eine zweite Klasse Manager wird von Employee abgeleitet. XmlArrayItemAttribute gibt an, dass XmlSerializer sowohl Employee-Objekte als auch Manager-Objekte in das Array einfügen kann. Im Beispiel wird die IsNullable-Eigenschaft festgelegt. Dadurch wird XmlSerializer angewiesen, die auf NULL (Nothing in Visual Basic) festgelegten xsi:nil-Attributobjekte im Array nicht zu generieren.

Option Explicit
Option Strict

Imports System
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
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();

   }
}
#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

public ref class Employee
{
public:
   String^ Name;
};

public ref class Manager: public Employee
{
public:
   int Level;
};

public ref class Group
{
public:

   [XmlArray(IsNullable=true)]
   [XmlArrayItem(Manager::typeid,IsNullable=false),
   XmlArrayItem(Employee::typeid,IsNullable=false)]
   array<Employee^>^Employees;
};

void SerializeObject( String^ filename )
{
   XmlSerializer^ s = gcnew XmlSerializer( Group::typeid );

   // To write the file, a TextWriter is required.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Creates the object to serialize.
   Group^ group = gcnew Group;

   // Creates a null Manager object.
   Manager^ mgr = nullptr;

   // Creates a null Employee object.
   Employee^ y = nullptr;
   array<Employee^>^temp = {mgr,y};
   group->Employees = temp;

   // Serializes the object and closes the TextWriter.
   s->Serialize( writer, group );
   writer->Close();
}

int main()
{
   SerializeObject( "TypeDoc.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;

public class Group
{
    /** @attribute XmlArray(IsNullable = true)
     */
    /** @attribute XmlArrayItem(Manager.class, IsNullable = false)
        @attribute XmlArrayItem(Employee.class, IsNullable = false)
     */
    public Employee employees[];
} //Group

public class Employee
{
    public String name;
} //Employee

public class Manager extends Employee
{
    public int level;
} //Manager

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        test.SerializeObject("TypeDoc.xml");
    } //main

    public void SerializeObject(String filename)
    {
        XmlSerializer s = new XmlSerializer(Group.class.ToType());

        // 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[] { mgr, y };

        // Serializes the object and closes the TextWriter.
        s.Serialize(writer, group);
        writer.Close();
    } //SerializeObject 
} //Run

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

XmlArrayItemAttribute-Klasse
XmlArrayItemAttribute-Member
System.Xml.Serialization-Namespace