XmlTextWriter.Formatting 属性

定义

指示如何对输出进行格式设置。

public:
 property System::Xml::Formatting Formatting { System::Xml::Formatting get(); void set(System::Xml::Formatting value); };
public System.Xml.Formatting Formatting { get; set; }
member this.Formatting : System.Xml.Formatting with get, set
Public Property Formatting As Formatting

属性值

Formatting

Formatting 值之一。 默认值为 Formatting.None(无特殊格式)。

示例

以下示例写入 XML 片段。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   //Create a writer to write XML to the console.
   XmlTextWriter^ writer = nullptr;
   writer = gcnew 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();
}
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

注解

备注

从 .NET Framework 2.0 开始,我们建议使用XmlWriter.Create方法和XmlWriterSettings类来创建XmlWriter实例,以利用新功能。

Indented如果已设置此选项,则使用IndentationIndentChar属性缩进子元素。 仅缩进元素内容。 以下 C# 代码写出 HTML 元素,包括混合内容:

XmlTextWriter w = new XmlTextWriter(Console.Out);   
 w.Formatting = Formatting.Indented;   
 w.WriteStartElement("ol");   
 w.WriteStartElement("li");   
 w.WriteString("The big "); // This means "li" now has a mixed content model.  
 w.WriteElementString("b", "E");   
 w.WriteElementString("i", "lephant");   
 w.WriteString(" walks slowly.");   
 w.WriteEndElement();   
 w.WriteEndElement();  

上述代码生成以下输出:

<ol>   
  <li>The big <b>E</b><i>lephant</i> walks slowly.</li>   
</ol>  

当 HTML 中查看此内容时,粗体和斜体元素之间不会显示空格。 事实上,在此示例中,如果在这些元素之间添加了缩进,“大象”一词将错误地断开。

备注

写入任何文本内容,不包括 String.Empty 该元素进入混合内容模式。 子元素不继承此“混合”模式状态。 “mixed”元素的子元素确实会缩进,除非它还包含“mixed”内容。 元素内容混合内容 根据这些术语的 XML 1.0 定义定义定义。

适用于

另请参阅