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 that indicates whether the XmlSerializer must serialize a member as an empty XML tag with the xsi:nil attribute set to true.

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

public bool IsNullable { get; set; }
/** @property */
public boolean get_IsNullable ()

/** @property */
public void set_IsNullable (boolean value)

public function get IsNullable () : boolean

public function set IsNullable (value : boolean)

Property Value

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

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 in Visual Basic), the XmlSerializer generates the following XML code.

 <MyStringArray xsi:nil = "true" />

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

NoteNote

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

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).

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

   }
}


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

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

Show:
© 2015 Microsoft