Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

XmlArrayItemAttribute.IsNullable Property

Gets or sets a value indicating whether the XmlSerializer should serialize a member as an empty XML tag with the xsi:nil attribute set to true.

[Visual Basic]
Public Property IsNullable As Boolean
[C#]
public bool IsNullable {get; set;}
[C++]
public: __property bool get_IsNullable();
public: __property void set_IsNullable(bool);
[JScript]
public function get IsNullable() : Boolean;
public function set IsNullable(Boolean);

Property Value

true if the XmlSerializer generates the xsi:nil attribute; otherwise, false, and no instance is generated. The default is true.

Remarks

The XML schema specification for structures allows an XML document to explicitly signal that an element's content is missing. Such an element contains the attribute xsi:nil set to true. For more information, see the World Wide Web Consortium (www.w3.org) specification titled "XML Schema Part 1: Structures."

If the IsNullable property is true, the xsi:nil attribute is generated for class members that have been set to a null reference (Nothing in Visual Basic). For example, if you set a field named MyStringArray to a null reference (Nothing), the XmlSerializer generates the following XML code.

<MyStringArray xsi:nil = "true" />

If the IsNullable property is false, no XML element is generated.

Note   You cannot apply the IsNullable property to a member typed as a value type because a value type cannot contain a null reference (Nothing).

Example

[Visual Basic, C#, C++] The following example serializes a class named Group, which contains a field named Employees that returns an array of Employee objects. A second class named Manager derives from Employee. An XmlArrayItemAttribute specifies that the XmlSerializer can insert both Employee and Manager objects into the array. The example sets the IsNullable property, thereby telling the XmlSerializer not to generate the xsi:nil attribute objects in the array set to a null reference (Nothing in Visual Basic).

[Visual Basic] 
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


[C#] 
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();

   }
}


[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

public __gc class Employee
{
public:
   String* Name;
};

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

public __gc class Group
{   
public:
   [XmlArray(IsNullable = true)]
   [XmlArrayItem(__typeof(Manager), IsNullable = false),
      XmlArrayItem(__typeof(Employee), IsNullable = false)]
   Employee* Employees[];
};   

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 = 0;

   // Creates a null Employee object.
   Employee* y = 0;

   Employee* temp[] = {mgr, y};      
   group->Employees = temp;

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

}

int main()
{
   SerializeObject(S"TypeDoc.xml");
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

XmlArrayItemAttribute Class | XmlArrayItemAttribute Members | System.Xml.Serialization Namespace

Show:
© 2014 Microsoft