XmlTextWriter.Indentation XmlTextWriter.Indentation XmlTextWriter.Indentation XmlTextWriter.Indentation Property

Definizione

Ottiene o imposta il numero di oggetti IndentChars da scrivere per ciascun livello nella gerarchia quando la proprietà Formatting è impostata su Formatting.Indented.Gets or sets how many IndentChars to write for each level in the hierarchy when Formatting is set to 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

Valore della proprietà

Numero di oggetti IndentChars per ciascun livello.Number of IndentChars for each level. Il valore predefinito è 2.The default is 2.

Eccezioni

Impostazione della proprietà su un valore negativo.Setting this property to a negative value.

Esempi

Nell'esempio seguente viene scritto un frammento XML.The following example writes an XML fragment.

#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

Commenti

Nota

A partire da .NET Framework 2,0, è consigliabile creare XmlWriter istanze usando il XmlWriter.Create metodo e la XmlWriterSettings classe per sfruttare le nuove funzionalità.Starting with the .NET Framework 2.0, we recommend that you create XmlWriter instances by using the XmlWriter.Create method and the XmlWriterSettings class to take advantage of new functionality.

Il rientro viene eseguito sui tipi di nodo DocumentTypeseguenti Element: Comment, ProcessingInstruction,, CDATASectione.Indentation is performed on following node types: DocumentType, Element, Comment, ProcessingInstruction, and CDATASection. Tutti gli altri tipi di nodo non sono interessati.All other node types are not affected. XmlTextWriter Non rientra nel subset DTD interno.The XmlTextWriter does not indent the internal DTD subset. Tuttavia, è possibile eseguire le operazioni seguenti per applicare la formattazione al subset DTD interno.However, you could do the following to apply formatting to the internal DTD subset.

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

Si applica a

Vedi anche