Freigeben über


XmlRootAttribute.IsNullable-Eigenschaft

Ruft einen Wert ab, der angibt, ob XmlSerializer einen auf NULL (Nothing in Visual Basic) festgelegten Member in das auf true festgelegte xsi:nil-Attribut serialisieren muss, oder legt diesen fest.

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

Syntax

'Declaration
Public Property IsNullable As Boolean
'Usage
Dim instance As XmlRootAttribute
Dim value As Boolean

value = instance.IsNullable

instance.IsNullable = value
public bool IsNullable { get; set; }
public:
property bool IsNullable {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_IsNullable ()

/** @property */
public void set_IsNullable (boolean value)
public function get IsNullable () : boolean

public function set IsNullable (value : boolean)

Eigenschaftenwert

true, wenn XmlSerializer das xsi:nil-Attribut generiert, andernfalls false.

Hinweise

Die XML-Schemaspezifikation für Strukturen ermöglicht es einem XML-Dokument, explizit zu signalisieren, dass der Inhalt eines Elements fehlt. Das Attribut xsi:nil eines solchen Elements ist auf true festgelegt. Weitere Informationen finden Sie in der W3C-Spezifikation XML Schema Part 1: Structures unter http://www.w3.org/TR/xmlschema-1/.

Wenn die IsNullable-Eigenschaft auf true festgelegt wurde, wird das xsi:nil-Attribut wie im folgenden XML-Code gezeigt generiert.

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

Wenn die IsNullable-Eigenschaft false ist, wird wie im folgenden Code ein leeres Element erstellt:

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

Beispiel

Im folgenden Beispiel wird die Klasse Group serialisiert. Im Beispiel wird XmlRootAttribute auf die Klasse angewendet und die IsNullable-Eigenschaft auf false festgelegt.

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
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 <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 ref class Group
{
public:
   String^ Name;
};

void SerializeObject( String^ filename )
{
   XmlSerializer^ s = gcnew XmlSerializer( Group::typeid );

   // Writing the file requires a TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Create the object to serialize.
   Group^ mygroup = nullptr;

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

int main()
{
   Console::WriteLine( "Running" );
   SerializeObject( "NullDoc.xml" );
}
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

Plattformen

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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

XmlRootAttribute-Klasse
XmlRootAttribute-Member
System.Xml.Serialization-Namespace