XmlTextWriter 的 XML 输出格式设置

XmlTextWriter 的 XML 输出格式设置由若干共同控制文档输出的属性组成。

XmlTextWriter 的 XML 输出格式设置由若干共同控制文档输出的属性组成。 输出格式设置属性为:

  • 格式

  • IndentChar

  • Indentation

  • QuoteChar

设置输出格式

Formatting 属性具有有效值 NoneIndented,其中 None 为默认值。 当 None 为选定值时,忽略 IndentCharIndentation 属性,并且不进行格式设置。 如果 Formatting 属性设置为 Indented,则应用程序查看 Indentation 属性以了解为层次结构中的每一层写出的 IndentChars 数目,然后 IndentChars 指定用于缩进的字符。 如果 Formatting 属性设置为 Indented,则 Indentation 的默认值是为层次结构中的每一层都写出 2 个 IndentChars,并且 IndentChars 的默认值是空格。 如果 Formatting 设置为 Indented,则子元素根据 IndentationIndentChar 值缩进。 由 XmlTextWriter 执行的缩进取决于节点类型。 受 Indentation 属性影响的节点为:

  • DocumentType

  • Element

  • Comment

  • 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 属性确定用于将属性值括起来的字符。 有效值为:

  • 单引号 (&#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