Freigeben über


XmlTextWriter.Formatting-Eigenschaft

Gibt die Formatierung der Ausgabe an.

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

Syntax

'Declaration
Public Property Formatting As Formatting
'Usage
Dim instance As XmlTextWriter
Dim value As Formatting

value = instance.Formatting

instance.Formatting = value
public Formatting Formatting { get; set; }
public:
property Formatting Formatting {
    Formatting get ();
    void set (Formatting value);
}
/** @property */
public Formatting get_Formatting ()

/** @property */
public void set_Formatting (Formatting value)
public function get Formatting () : Formatting

public function set Formatting (value : Formatting)

Eigenschaftenwert

Einer der Formatting-Werte. Der Standardwert ist Formatting.None (keine besondere Formatierung).

Hinweise

Hinweis

Die empfohlene Vorgehensweise für die Version Microsoft .NET Framework, Version 2.0 besteht darin, mithilfe der System.Xml.XmlWriter.Create-Methode und der XmlWriterSettings-Klasse XmlWriter-Instanzen zu erstellen. So können Sie alle neuen Features dieser Version in vollem Umfang nutzen. Weitere Informationen finden Sie unter Erstellen von XML-Writern.

Wenn die Indented-Option festgelegt ist, werden untergeordnete Elemente mithilfe der Indentation-Eigenschaft und der IndentChar-Eigenschaft eingezogen. Nur der Elementinhalt wird eingezogen. Der folgende C#-Code schreibt HTML-Elemente mit gemischtem Inhalt:

 XmlTextWriter w = new XmlTextWriter(Console.Out); 
  w.Formatting = Formatting.Indented; 
  w.WriteStartElement("ol"); 
  w.WriteStartElement("li"); 
  w.WriteString("The big "); // This means "li" now has a mixed content model.
  w.WriteElementString("b", "E"); 
  w.WriteElementString("i", "lephant"); 
  w.WriteString(" walks slowly."); 
  w.WriteEndElement(); 
  w.WriteEndElement();

Der obige Code erzeugt die folgende Ausgabe:

 <ol> 
   <li>The big <b>E</b><i>lephant</i> walks slowly.</li> 
 </ol>

In HTML wird zwischen fett und kursiv formatierten Elementen kein Leerraum angezeigt. Befände sich im Beispiel zwischen diesen Elementen ein Einzug, wäre das Wort "Elephant" an unzulässiger Stelle unterbrochen.

Hinweis

Beim Schreiben beliebigen Textinhalts, einschließlich String.Empty, wechselt dieses Element in den Modus mit gemischtem Inhalt. Untergeordnete Elemente erben diesen gemischten Modus nicht. Für untergeordnete Elemente eines Elements mit gemischtem Inhalt erfolgen Einzüge nur dann, wenn die untergeordneten Elemente ebenfalls gemischten Inhalt enthalten. Die Definitionen von Elementinhalt (http://www.w3.org/TR/1998/REC-xml-19980210#sec-element-content) und gemischtem Inhalt (http://www.w3.org/TR/1998/REC-xml-19980210#sec-mixed-content) entsprechen den XML 1.0-Definitionen dieser Begriffe.

Beispiel

Im folgenden Beispiel wird ein XML-Fragment geschrieben.

Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        'Create a writer to write XML to the console.
        Dim writer As XmlTextWriter = Nothing
        writer = New XmlTextWriter(Console.Out)
        
        'Use indentation for readability.
        writer.Formatting = Formatting.Indented
        writer.Indentation = 4
        
        'Write an element (this one is the root).
        writer.WriteStartElement("book")
        
        'Write the title element.
        writer.WriteStartElement("title")
        writer.WriteString("Pride And Prejudice")
        writer.WriteEndElement()
        
        'Write the close tag for the root element.
        writer.WriteEndElement()
        
        'Write the XML to file and close the writer.
        writer.Close()
    End Sub 'Main 
End Class 'Sample
using System;
using System.IO;
using System.Xml;

public class Sample
{
  
  public static void Main()
  {
     //Create a writer to write XML to the console.
     XmlTextWriter writer = null;
     writer = new XmlTextWriter (Console.Out);

     //Use indentation for readability.
     writer.Formatting = Formatting.Indented;
     writer.Indentation = 4;
        
     //Write an element (this one is the root).
     writer.WriteStartElement("book");

     //Write the title element.
     writer.WriteStartElement("title");
     writer.WriteString("Pride And Prejudice");
     writer.WriteEndElement();

     //Write the close tag for the root element.
     writer.WriteEndElement();
             
     //Write the XML to file and close the writer.
     writer.Close();  

  }

}
#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   //Create a writer to write XML to the console.
   XmlTextWriter^ writer = nullptr;
   writer = gcnew XmlTextWriter( Console::Out );
   
   //Use indentation for readability.
   writer->Formatting = Formatting::Indented;
   writer->Indentation = 4;
   
   //Write an element (this one is the root).
   writer->WriteStartElement( "book" );
   
   //Write the title element.
   writer->WriteStartElement( "title" );
   writer->WriteString( "Pride And Prejudice" );
   writer->WriteEndElement();
   
   //Write the close tag for the root element.
   writer->WriteEndElement();
   
   //Write the XML to file and close the writer.
   writer->Close();
}
import System.*;
import System.IO.*;
import System.Xml.*;

public class Sample
{
    public static void main(String[] args)
    {
        //Create a writer to write XML to the console.
        XmlTextWriter writer = null;
        writer = new XmlTextWriter(Console.get_Out());

        //Use indentation for readability.
        writer.set_Formatting(Formatting.Indented);
        writer.set_Indentation(4);

        //Write an element (this one is the root).
        writer.WriteStartElement("book");

        //Write the title element.
        writer.WriteStartElement("title");
        writer.WriteString("Pride And Prejudice");
        writer.WriteEndElement();

        //Write the close tag for the root element.
        writer.WriteEndElement();

        //Write the XML to file and close the writer.
        writer.Close();
    } //main 
} //Sample

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

XmlTextWriter-Klasse
XmlTextWriter-Member
System.Xml-Namespace
Formatting-Enumeration