XmlTextWriter.Indentation Właściwość

Definicja

Pobiera lub ustawia liczbę wcięćChars do zapisania dla każdego poziomu w hierarchii, gdy Formatting jest ustawiona wartość 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

Wartość właściwości

IndentChars Liczba dla każdego poziomu. Wartość domyślna to 2.

Wyjątki

Ustawienie tej właściwości na wartość ujemną.

Przykłady

Poniższy przykład zapisuje fragment 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

Uwagi

Uwaga

Począwszy od .NET Framework 2.0, zalecamy tworzenie XmlWriter wystąpień przy użyciu XmlWriter.Create metody i XmlWriterSettings klasy, aby korzystać z nowych funkcji.

Wcięcie jest wykonywane na następujących typach węzłów: DocumentType, , ElementComment, ProcessingInstructioni CDATASection. Nie ma to wpływu na wszystkie inne typy węzłów. Element XmlTextWriter nie powoduje wcięcia wewnętrznego podzestawu DTD. Można jednak wykonać następujące czynności, aby zastosować formatowanie do wewnętrznego podzestawu DTD.

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

Dotyczy

Zobacz też