語言

XmlWriterSettings.IndentChars 屬性

定義

在縮排時取得或設定字串的使用。 當屬性 Indent 設定 true為 時,此設定會使用。

public:
 property System::String ^ IndentChars { System::String ^ get(); void set(System::String ^ value); };
public string IndentChars { get; set; }
member this.IndentChars : string with get, set
Public Property IndentChars As String

屬性值

縮排時要用的字串。 此值可設定為任意字串值。 然而,為了確保 XML 的有效性,你應該只指定有效的空白字元,例如空格字元、制表符、回車符或換行符。 預設值為兩個空格。

例外狀況

賦予 的 IndentChars 值為 null。

範例

以下範例建立一個 XmlWriter 物件,使用 TAB 字元進行縮排。

using System;
using System.IO;
using System.Xml;
using System.Text;

public class Sample {

  public static void Main() {

    XmlWriter writer = null;

    try {

       // Create an XmlWriterSettings object with the correct options.
       XmlWriterSettings settings = new XmlWriterSettings();
       settings.Indent = true;
       settings.IndentChars = ("\t");
       settings.OmitXmlDeclaration = true;

       // Create the XmlWriter object and write some content.
       writer = XmlWriter.Create("data.xml", settings);
       writer.WriteStartElement("book");
       writer.WriteElementString("item", "tesing");
       writer.WriteEndElement();
    
       writer.Flush();
     }
     finally  {
        if (writer != null)
          writer.Close();
     }
  }
}
Imports System.IO
Imports System.Xml
Imports System.Text

Public Class Sample 

  Public Shared Sub Main() 
  
    Dim writer As XmlWriter = Nothing

    Try 

       ' Create an XmlWriterSettings object with the correct options. 
       Dim settings As XmlWriterSettings = New XmlWriterSettings()
       settings.Indent = true
       settings.IndentChars = (ControlChars.Tab)
       settings.OmitXmlDeclaration = true

       ' Create the XmlWriter object and write some content.
       writer = XmlWriter.Create("data.xml", settings)
       writer.WriteStartElement("book")
       writer.WriteElementString("item", "tesing")
       writer.WriteEndElement()
    
       writer.Flush()

      Finally
         If Not (writer Is Nothing) Then
            writer.Close()
         End If
      End Try

   End Sub 
End Class

備註

此特性僅適用於 XmlWriter 輸出文字內容的實例;否則此設定將被忽略。 如果縮排字元會導致無效 XML 輸出,則 XmlWriter 會拋出例外。

適用於