XmlWriterSettings.IndentChars Property

Definition

取得或設定縮排時使用的字元字串。Gets or sets the character string to use when indenting. Indent 屬性設為 true 時會使用這項設定。This setting is used when the Indent property is set to 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

Property Value

String

縮排時使用的字元字串。The character string to use when indenting. 它可以設為任何字串值。This can be set to any string value. 不過,若要確保有效的 XML,您應該只指定有效的空白字元 (例如,空格字元、定位字元、歸位字元或換行符號)。However, to ensure valid XML, you should specify only valid white space characters, such as space characters, tabs, carriage returns, or line feeds. 預設值為兩個空格。The default is two spaces.

Exceptions

指派至 IndentChars 的值為 nullThe value assigned to the IndentChars is null.

Examples

下列範例會建立使用 TAB 字元進行縮排的 XmlWriter 物件。The following example creates an XmlWriter object that uses the TAB character for indentation.

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 

Remarks

此屬性僅適用于輸出文字內容的 XmlWriter 實例;否則,會忽略此設定。This property only applies to XmlWriter instances that output text content; otherwise, this setting is ignored. 如果縮排字元會導致不正確 XML,XmlWriter 會擲回例外狀況。The XmlWriter throws an exception if the indent characters would result in invalid XML.

Applies to