XmlTextWriter.Formatting 属性

定义

指示如何对输出进行格式设置。Indicates how the output is formatted.

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 值之一。One of the Formatting values. 默认值为 Formatting.None(无特殊格式)。The default is Formatting.None (no special formatting).

示例

下面的示例写入 XML 片段。The following example writes an XML fragment.

#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 使用 XmlWriter.Create 方法和类创建实例, XmlWriterSettings 以利用新功能。Starting with the .NET Framework 2.0, we recommend that you create XmlWriter instances by using the XmlWriter.Create method and the XmlWriterSettings class to take advantage of new functionality.

如果 Indented 设置了选项,则使用和属性缩进子 Indentation 元素 IndentCharIf the Indented option is set, child elements are indented using the Indentation and IndentChar properties. 仅缩进元素内容。Only element content is indented. 下面的 c # 代码写出 HTML 元素,包括混合内容:The following C# code writes out HTML elements including mixed content:

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();  

上面的代码生成以下输出:The above code produces the following output:

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

在 HTML 中查看此内容时,粗体和斜体元素之间不会出现空格。When this is viewed in HTML no white space appears between the bold and italic elements. 事实上,在此示例中,如果在这些元素之间添加缩进,则 "大象" 一词将会错误地损坏。In fact, in this example, if indenting was added between these elements the word "Elephant" would be incorrectly broken.

备注

写入任何文本内容,不包括将 String.Empty 该元素放入混合内容模式。Writing any text content, excluding String.Empty puts that element into mixed content mode. 子元素不会继承此 "混合" 模式状态。Child elements do not inherit this "mixed" mode status. "Mixed" 元素的子元素会缩进,除非它还包含 "混合" 内容。A child element of a "mixed" element does indenting, unless it is also contains "mixed" content. 根据这些术语的 XML 1.0 定义定义元素内容混合内容Element content and mixed content are defined according to the XML 1.0 definitions of these terms.

适用于

另请参阅