XmlTextWriter.Indentation Propriedade
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.
Obtém ou define quantos IndentChars gravar para cada nível da hierarquia quando Formatting está definido como Formatting.Indented
.
public:
property int Indentation { int get(); void set(int value); };
public int Indentation { get; set; }
member this.Indentation : int with get, set
Public Property Indentation As Integer
Número de IndentChars
para cada nível. O padrão é 2.
Definir essa propriedade para um valor negativo.
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
Nota
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.
O recuo é executado nos seguintes tipos de nó: DocumentType
, , Element
, Comment
e ProcessingInstruction``CDATASection
. Todos os outros tipos de nó não são afetados. O XmlTextWriter
subconjunto DTD interno não recuou. No entanto, você pode fazer o seguinte para aplicar a formatação ao subconjunto DTD interno.
String name = "Employees";
String pubid = null;
String sysid = null;
String subset =
@"
<!ELEMENT Employees (Employee)+>
<!ELEMENT Employee EMPTY>
<!ATTLIST Employee firstname CDATA #REQUIRED>
<!ENTITY Company 'Microsoft'>]>
";
XmlTextWriter tw = new XmlTextWriter(Console.Out);
tw.WriteDocType(name, pubid, sysid, subset);