共用方式為


XmlWriterSettings.Indent 屬性

定義

取得或設定值,指出是否要縮排項目。

public:
 property bool Indent { bool get(); void set(bool value); };
public bool Indent { get; set; }
member this.Indent : bool with get, set
Public Property Indent As Boolean

屬性值

Boolean

true 表示在新行和縮排上寫入個別項目,否則為 false。 預設為 false

範例

下列範例會 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 輸出文字內容的實例,否則會忽略此設定。

只要元素不包含混合內容,元素即會縮排。 WriteString呼叫 或 WriteWhitespace 方法以寫出混合專案內容之後,就會 XmlWriter 停止縮排。 縮排會在混合內容專案關閉後繼續。

適用於