XmlTextWriter.Formatting Właściwość

Definicja

Wskazuje sposób formatowania danych wyjściowych.

public:
 property System::Xml::Formatting Formatting { System::Xml::Formatting get(); void set(System::Xml::Formatting value); };
public System.Xml.Formatting Formatting { get; set; }
member this.Formatting : System.Xml.Formatting with get, set
Public Property Formatting As Formatting

Wartość właściwości

Formatting

Jedna z Formatting wartości. Wartość domyślna to Formatting.None (bez specjalnego formatowania).

Przykłady

Poniższy przykład zapisuje fragment XML.

#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();
}
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();
  }
}
Option Explicit
Option Strict

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
End Class

Uwagi

Uwaga

Począwszy od .NET Framework 2.0, zalecamy utworzenie XmlWriter wystąpień przy użyciu metody i XmlWriterSettings klasy, aby korzystać z XmlWriter.Create nowych funkcji.

Jeśli opcja jest ustawiona Indented , elementy podrzędne są wcięcie przy użyciu Indentation właściwości i IndentChar . Tylko zawartość elementu jest wcięta. Następujący kod w języku C# zapisuje elementy HTML, w tym zawartość mieszaną:

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

Powyższy kod generuje następujące dane wyjściowe:

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

Gdy ta opcja jest wyświetlana w kodzie HTML, między elementami pogrubionym i kursywą nie pojawia się odstęp. W tym przykładzie, jeśli wcięcie zostało dodane między tymi elementami, słowo "Elephant" byłoby niepoprawnie przerwane.

Uwaga

Pisanie dowolnej zawartości tekstowej z wyłączeniem String.Empty tego elementu powoduje przełączanie tego elementu do trybu zawartości mieszanej. Elementy podrzędne nie dziedziczą tego stanu trybu mieszanego. Element podrzędny elementu "mieszanego" wykonuje wcięcie, chyba że zawiera również zawartość mieszaną. Zawartość elementu i zawartość mieszana są definiowane zgodnie z definicjami xml 1.0 tych terminów.

Dotyczy

Zobacz też