다음을 통해 공유


XmlTextWriter.Indentation 속성

정의

FormattingFormatting.Indented로 설정될 때 계층구조의 각 수준에 작성할 IndentChars 수를 가져오거나 설정합니다.

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

속성 값

Int32

각 수준의 IndentChars 수입니다. 기본값은 2입니다.

예외

이 속성을 음의 값으로 설정하는 경우

예제

다음 예제에서는 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

설명

참고

.NET Framework 2.0부터 메서드와 XmlWriterSettings 클래스를 사용하여 XmlWriter.Create 새 기능을 활용하여 인스턴스를 만드는 XmlWriter 것이 좋습니다.

들여쓰기는 다음 노드 유형DocumentType, , ProcessingInstruction``Element``CommentCDATASection에 대해 수행됩니다. 다른 모든 노드 형식은 영향을 받지 않습니다. 내부 XmlTextWriter DTD 하위 집합을 들여쓰지 않습니다. 그러나 다음을 수행하여 내부 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);  

적용 대상

추가 정보