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 null 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 Value

Type: System.Boolean
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();
   }
}
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
using namespace System::Xml;

// Apply the XmlRootAttribute and set the IsNullable property to false.
[XmlRoot(IsNullable = false)]
public __gc class Group
{   
public:
   String* Name;
};   

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

   // Serialize the object, and close the TextWriter.
   s->Serialize(writer, mygroup);
   writer->Close();
}

int main()
{
   Console::WriteLine(S"Running");
   SerializeObject(S"NullDoc.xml");
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

Show:
© 2014 Microsoft