XmlTextWriter.Indentation 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置在设置为 Formatting 时Formatting.Indented,层次结构中每个级别要写入的 IndentChar 数。
public:
property int Indentation { int get(); void set(int value); };
public int Indentation { get; set; }
member this.Indentation : int with get, set
Public Property Indentation As Integer
属性值
IndentChars每个级别的数量。 默认值为 2。
例外
将此属性设置为负值。
示例
以下示例写入 XML 片段。
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();
}
}
Option Explicit
Option Strict
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
End Class
注解
注释
建议使用XmlWriter方法和XmlWriter.Create类创建XmlWriterSettings实例,以利用新功能。
缩进在以下节点类型上执行:DocumentType、、ElementComment、ProcessingInstruction和CDATASection。 所有其他节点类型都不会受到影响。 不会 XmlTextWriter 缩进内部 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);