XmlTextWriter.Indentation プロパティ

定義

FormattingFormatting.Indented に設定するときに、階層構造のレベルごとに書き込む IndentChar の数を取得または設定します。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

プロパティ値

各レベルの IndentChars の数。Number of IndentChars for each level. 既定値は 2 です。The default is 2.

例外

このプロパティを負の値に設定。Setting this property to a negative value.

次の例では、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

注釈

注意

.NET Framework 2.0 以降では、新しい機能を利用するXmlWriterために、 XmlWriter.CreateメソッドとXmlWriterSettingsクラスを使用してインスタンスを作成することをお勧めします。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.

DocumentTypeインデントはElement、、、 、ProcessingInstruction、およびCDATASectionの各ノード型で実行されます。 CommentIndentation is performed on following node types: DocumentType, Element, Comment, ProcessingInstruction, and CDATASection. その他のすべてのノード型は影響を受けません。All other node types are not affected. XmlTextWriter 、内部 DTD サブセットをインデントしません。The XmlTextWriter does not indent the internal DTD subset. ただし、内部の DTD サブセットに書式を適用するには、次の操作を実行します。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);  

適用対象

こちらもご覧ください