XmlTextWriter.Formatting Propriedade

Definição

Indica como a saída está 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 de Propriedade

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

Exemplos

O exemplo seguinte escreve um fragmento XML.

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

Observações

Note

Recomendamos que crie instâncias XmlWriter usando o método XmlWriter.Create e a classe XmlWriterSettings para aproveitar a nova funcionalidade.

Se a Indented opção estiver definida, os elementos filhos são indentados usando as Indentation propriedades e.IndentChar Apenas o conteúdo dos elementos é indentado. O seguinte código C# escreve 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 isto é visualizado em HTML, não aparece espaço em branco entre os elementos a negrito e itálico. De facto, neste exemplo, se fosse adicionado um recuo entre estes elementos, a palavra "Elefante" seria quebrada incorretamente.

Note

Escrever qualquer conteúdo textual, excluindo String.Empty , coloca esse elemento em modo de conteúdo misto. Os elementos filhos não herdam este estado de modo "misto". Um elemento filho de um elemento "misto" faz indentação, a menos que também contenha conteúdo "misto". O conteúdo elemental e o conteúdo misto são definidos de acordo com as definições XML 1.0 destes termos.

Aplica-se a

Ver também