XmlTextWriter.Formatting Propriedade
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
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
Um dos Formatting valores. O padrão é Formatting.None (nenhuma formatação especial).
Exemplos
O exemplo a seguir grava 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
Comentários
Observação
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 estiver definida, os elementos filho serão recuados usando as propriedades eIndentChar.Indentation 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 foi 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.