XmlTextWriter.Indentation Propriedade

Definição

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

Valor da propriedade

Int32

Número de IndentChars para cada nível. O padrão é 2.

Exceções

Definir essa propriedade para um valor negativo.

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.

O recuo é executado nos seguintes tipos de nó: DocumentType, , Element, Commente 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);  

Aplica-se a

Confira também