Windows Dev Center

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.

XmlRootAttribute.IsNullable Property

Gets or sets a value that indicates whether the XmlSerializer must serialize a member that is set to a null reference (Nothing in Visual Basic) into 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.

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 http://www.w3.org/TR/xmlschema-1/ specification named XML Schema Part 1: Structures.

If the IsNullable property is set to true, the xsi:nil attribute is generated as shown in the following XML:

 <?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" />

If the IsNullable property is false, an empty element is created as shown in the following code:

 <?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" />

The following example serializes a class named Group. The example applies the XmlRootAttribute to the class, and sets the IsNullable property to false.

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

import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
import System.Xml.*;

// Apply the XmlRootAttribute and set the IsNullable property to false.
/** @attribute XmlRoot(IsNullable = false)
 */
public class Group
{
    public String name;
} //Group

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

    public void SerializeObject(String fileName)
    {
        XmlSerializer s = new XmlSerializer(Group.class.ToType());
        // 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();
    } //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