XmlWriterSettings.IndentChars 属性
定义
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
属性值
缩进时要使用的字符串。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.
例外
分配给 IndentChars 的值为 null。The value assigned to the IndentChars is null.
示例
下面的示例创建一个 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
注解
此属性仅适用于 XmlWriter 输出文本内容的实例; 否则,将忽略此设置。This property only applies to XmlWriter instances that output text content; otherwise, this setting is ignored. XmlWriter如果缩进字符将导致无效的 XML,则会引发异常。The XmlWriter throws an exception if the indent characters would result in invalid XML.