XmlTextWriter.Formatting Propriedade

Definição

Indica como a saída é formatada.

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

Valor da propriedade

Formatting

Um dos valores de Formatting. O padrão é Formatting.None (sem formatação especial).

Exemplos

O exemplo a seguir grava um fragmento 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

Comentários

Observação

A partir do .NET Framework 2.0, recomendamos que você crie XmlWriter instâncias usando o XmlWriter.Create método e a XmlWriterSettings classe para aproveitar a nova funcionalidade.

Se a opção Indented for definida, os elementos filho serão recuados usando o e IndentChar as Indentation propriedades. Somente o conteúdo do elemento é recuado. O código C# a seguir grava elementos HTML, incluindo conteúdo misto:

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

O código acima produz a seguinte saída:

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

Quando isso é exibido em HTML, nenhum espaço em branco aparece entre os elementos em negrito e itálico. Na verdade, neste exemplo, se o recuo fosse adicionado entre esses elementos, a palavra "Elefante" seria incorretamente quebrada.

Observação

Escrever qualquer conteúdo de texto, excluindo String.Empty coloca esse elemento no modo de conteúdo misto. Os elementos filho não herdam esse status de modo "misto". Um elemento filho de um elemento "misto" recua, a menos que também contenha conteúdo "misto". O conteúdo do elemento e o conteúdo misto são definidos de acordo com as definições XML 1.0 desses termos.

Aplica-se a

Confira também