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 indicating whether the XmlSerializer should serialize a member that is set to a null reference (Nothing in Visual Basic) into 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.

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 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 below:

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

Example

[Visual Basic, C#, C++] The following example serializes a class named Group. The example applies the XmlRootAttribute to the class, and sets the IsNullable property to false.

[Visual Basic] 
Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml


' Apply the XmlRootAttribute and set the IsNullable property to false.
<XmlRoot(IsNullable := False)> _
Public Class Group
    Public Name As String
End Class


Public Class Run
    
    Public Shared Sub Main()
        Console.WriteLine("Running")
        Dim test As New Run()
        test.SerializeObject("NullDoc.xml")
    End Sub     
    
    Public Sub SerializeObject(ByVal filename As String)
        Dim s As New XmlSerializer(GetType(Group))
        
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Create the object to serialize.
        Dim mygroup As Group = Nothing
        
        ' Serialize the object, and close the TextWriter.
        s.Serialize(writer, mygroup)
        writer.Close()
    End Sub
End Class


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

[C++] 
#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");
}

[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

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

Show:
© 2015 Microsoft