共用方式為


以 XmlTextWriter 格式化的 XML 輸出

XmlTextWriter 格式化的 XML 輸出是由數個一起用來控制文件輸出的屬性組成。

XmlTextWriter 格式化的 XML 輸出是由數個一起用來控制文件輸出的屬性組成。 輸出格式屬性為:

  • Formatting

  • IndentChar

  • Indentation

  • QuoteChar

格式化輸出

Formatting 屬性有 NoneIndented 兩個有效值,None 是預設值。 當 None 為所選取的值時,IndentCharIndentation 屬性會被忽略,且不會進行格式化。 如果將 Formatting 屬性設定為 Indented,則應用程式會檢查 Indentation 屬性,以查看階層內的每一個層次要寫入 IndentChar 的數量,然後 IndentChar 會指定要用來縮排的字元。 如果將 Formatting 屬性設定為 Indented,則 Indentation 的預設值會對階層中的每一層次寫入兩個 IndentChar,且 IndentChar 的預設值是空格。 如果將 Formatting 設定為 Indented,則會依據 IndentationIndentChar 值來縮排項目子系。 XmlTextWriter 依據節點型別執行縮排。 Indentation 屬性所影響的節點為:

  • DocumentType

  • 項目

  • 註解

  • ProcessingInstruction

  • CDATASection

所有其他的節點型別並不會受到 Indentation 屬性的影響,因此不會對它們執行縮排。

文件類型定義 (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);

QuoteChar 屬性 (Property) 會判斷用來括住屬性 (Attribute) 值的字元。 有效值為:

  • 單引號 (&#39;)

  • 雙引號 (&#34;)

QuoteChar 的預設值是雙引號 (&#34;)。

範例

下列程式碼會寫入 XML 片段,將 Formatting 屬性設定為 Indented、縮排層次為 4 和一個縮排空白字元 (預設值)。

Option Explicit
Option Strict

Imports System
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 'Main 
End Class 'Sample
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();  
  }
}

請參閱

其他資源

使用 XmlWriter 寫入 XML